Which of the following demonstrate the correct syntax for the switch statement?

Which of the following demonstrate the correct syntax for the switch statement?

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:



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Joshua

Joshua

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.

James

James

Answers A and B are identical