switch
statement. A switch
statement tests a value and can have many case
statements which define various possible values. Statements are executed from the first matched case
value until a break
is encountered.
Here is a pseudocode example:
switch(num) {
case value1:
statement1;
break;
case value2:
statement2;
break;
...
case valueN:
statementN;
break;
}
case
values are tested with strict equality (===
). The break
tells JavaScript to stop executing statements. If the break
is omitted, the next statement will be executed.
val
and sets answer
for the following conditions:1
- "alpha"2
- "beta"3
- "gamma"4
- "delta"
caseInSwitch(1)
should have a value of "alpha"
testString: 'assert(caseInSwitch(1) === "alpha", ''caseInSwitch(1)
should have a value of "alpha"'');'
- text: caseInSwitch(2)
should have a value of "beta"
testString: 'assert(caseInSwitch(2) === "beta", ''caseInSwitch(2)
should have a value of "beta"'');'
- text: caseInSwitch(3)
should have a value of "gamma"
testString: 'assert(caseInSwitch(3) === "gamma", ''caseInSwitch(3)
should have a value of "gamma"'');'
- text: caseInSwitch(4)
should have a value of "delta"
testString: 'assert(caseInSwitch(4) === "delta", ''caseInSwitch(4)
should have a value of "delta"'');'
- text: You should not use any if
or else
statements
testString: 'assert(!/else/g.test(code) || !/if/g.test(code), ''You should not use any if
or else
statements'');'
- text: You should have at least 3 break
statements
testString: 'assert(code.match(/break/g).length > 2, ''You should have at least 3 break
statements'');'
```