switch
statement can be easier to write than many chained if
/else if
statements. The following:
```js
if (val === 1) {
answer = "a";
} else if (val === 2) {
answer = "b";
} else {
answer = "c";
}
```
can be replaced with:
```js
switch(val) {
case 1:
answer = "a";
break;
case 2:
answer = "b";
break;
default:
answer = "c";
}
```
if
/else if
statements into a switch
statement.
else
statements anywhere in the editor
testString: assert(!/else/g.test(code));
- text: You should not use any if
statements anywhere in the editor
testString: assert(!/if/g.test(code));
- text: You should have at least four break
statements
testString: assert(code.match(/break/g).length >= 4);
- text: chainToSwitch("bob")
should be "Marley"
testString: assert(chainToSwitch("bob") === "Marley");
- text: chainToSwitch(42)
should be "The Answer"
testString: assert(chainToSwitch(42) === "The Answer");
- text: "chainToSwitch(1)
should be \"There is no #1\""
testString: "assert(chainToSwitch(1) === \"There is no #1\");"
- text: chainToSwitch(99)
should be "Missed me by this much!"
testString: assert(chainToSwitch(99) === "Missed me by this much!");
- text: chainToSwitch(7)
should be "Ate Nine"
testString: assert(chainToSwitch(7) === "Ate Nine");
- text: chainToSwitch("John")
should be "" (empty string)
testString: assert(chainToSwitch("John") === "");
- text: chainToSwitch(156)
should be "" (empty string)
testString: assert(chainToSwitch(156) === "");
```