This commit adds the pre-existing challenge guide topics in the forum to the forntmatter of their challenge markdown files.
3.2 KiB
3.2 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
56533eb9ac21ba0edf2244ca | Using Objects for Lookups | 1 | https://scrimba.com/c/cdBk8sM | 18373 |
Description
switch
statement or an if/else
chain. This is most useful when you know that your input data is limited to a certain range.
Here is an example of a simple reverse alphabet lookup:
var alpha = {
1:"Z",
2:"Y",
3:"X",
4:"W",
...
24:"C",
25:"B",
26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"
var value = 2;
alpha[value]; // "Y"
Instructions
lookup
. Use it to look up val
and assign the associated string to the result
variable.
Tests
tests:
- text: <code>phoneticLookup("alpha")</code> should equal <code>"Adams"</code>
testString: assert(phoneticLookup("alpha") === 'Adams');
- text: <code>phoneticLookup("bravo")</code> should equal <code>"Boston"</code>
testString: assert(phoneticLookup("bravo") === 'Boston');
- text: <code>phoneticLookup("charlie")</code> should equal <code>"Chicago"</code>
testString: assert(phoneticLookup("charlie") === 'Chicago');
- text: <code>phoneticLookup("delta")</code> should equal <code>"Denver"</code>
testString: assert(phoneticLookup("delta") === 'Denver');
- text: <code>phoneticLookup("echo")</code> should equal <code>"Easy"</code>
testString: assert(phoneticLookup("echo") === 'Easy');
- text: <code>phoneticLookup("foxtrot")</code> should equal <code>"Frank"</code>
testString: assert(phoneticLookup("foxtrot") === 'Frank');
- text: <code>phoneticLookup("")</code> should equal <code>undefined</code>
testString: assert(typeof phoneticLookup("") === 'undefined');
- text: You should not modify the <code>return</code> statement
testString: assert(code.match(/return\sresult;/));
- text: You should not use <code>case</code>, <code>switch</code>, or <code>if</code> statements
testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,'')));
Challenge Seed
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
switch(val) {
case "alpha":
result = "Adams";
break;
case "bravo":
result = "Boston";
break;
case "charlie":
result = "Chicago";
break;
case "delta":
result = "Denver";
break;
case "echo":
result = "Easy";
break;
case "foxtrot":
result = "Frank";
}
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
Solution
function phoneticLookup(val) {
var result = "";
var lookup = {
alpha: "Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank"
};
result = lookup[val];
return result;
}