Which of the following demonstrate the correct syntax for the switch statement?
A.
var myName= “Paul”
switch (myName){
case “Peter”
document.write(“My name is Peter.
“)
break
case “Paul”
document.write(“My name is Paul.
“)
break: (default);
document.write(“I do not have a name.
“)
}
B.
var myName= “Paul”
switch (myName){
case “Peter”
document.write(“My name is Peter.
“)
break
case “Paul”
document.write(“My name is Paul.
“)
break: (default);
document.write(“I do not have a name.
“)
}
C.
var myName= “Paul”
switch (myName){
case (Peter)
document.write(“My name is Peter.
“)
break
case (Paul)
document.write(“My name is Paul.
“)
break;
case: (default);
document.write(“I do not have a name.
“)
}
D.
var myName= “Paul”
switch (myName){
case (Peter)
document.write(“My name is Peter.
“)
break
case (Paul)
document.write(“My name is Paul.
“)
break;
case: (default);
document.write(“I do not have a name.
“)
}
Explanation:
These all have horrible syntax…First off, you need a semicolon after every break. Second, you should have colons after each case, including the default, which is not in parenthesis.
Please fix this.
Answers A and B are identical