This was my answer. I did not take into account the else if statements.
var answer = prompt ("What is the value of 2+2?");
while (answer != "4"){
var answer = prompt ("No, try again. What is the value of 2 + 2?");
if (answer == "4")
break;
}
The solution provided by the book was more elegant. It used the while statement that if true, will continue to run until the break.
var answer;
while (true) {
answer = prompt("You! What is the value of 2 + 2?", "");
if (answer == "4") {
alert("You must be a genius or something.");
break;
}
else if (answer == "3" || answer == "5") {
alert("Almost!");
}
else {
alert("You're an embarrassment.");
}
}
No comments:
Post a Comment