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"
lookup
. Use it to look up val
and assign the associated string to the result
variable.
phoneticLookup("alpha")
should equal "Adams"
testString: 'assert(phoneticLookup("alpha") === "Adams", "phoneticLookup("alpha")
should equal "Adams"
");'
- text: phoneticLookup("bravo")
should equal "Boston"
testString: 'assert(phoneticLookup("bravo") === "Boston", "phoneticLookup("bravo")
should equal "Boston"
");'
- text: phoneticLookup("charlie")
should equal "Chicago"
testString: 'assert(phoneticLookup("charlie") === "Chicago", "phoneticLookup("charlie")
should equal "Chicago"
");'
- text: phoneticLookup("delta")
should equal "Denver"
testString: 'assert(phoneticLookup("delta") === "Denver", "phoneticLookup("delta")
should equal "Denver"
");'
- text: phoneticLookup("echo")
should equal "Easy"
testString: 'assert(phoneticLookup("echo") === "Easy", "phoneticLookup("echo")
should equal "Easy"
");'
- text: phoneticLookup("foxtrot")
should equal "Frank"
testString: 'assert(phoneticLookup("foxtrot") === "Frank", "phoneticLookup("foxtrot")
should equal "Frank"
");'
- text: phoneticLookup("")
should equal undefined
testString: 'assert(typeof phoneticLookup("") === "undefined", "phoneticLookup("")
should equal undefined
");'
- text: You should not modify the return
statement
testString: 'assert(code.match(/return\sresult;/), "You should not modify the return
statement");'
- text: 'You should not use case
, switch
, or if
statements'
testString: 'assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,"")), "You should not use case
, switch
, or if
statements"); '
```