I’m a newbie learning Javascript. In the following tutorial, I don`t understand
-
why value is initially set to null
-
if the value entered is 2, for example, how does the program know to display “You`re an embarassment” and not redisplay the initial question “What is the value of 2 + 2”?
The question/prompt is triggered when value != 4 so I would expect an answer of 2 to retrigger to question/prompt, but instead the program displays the message “You`re an embarassment”.
Can anyone explain?
var value = null;
while (value != "4") {
value = prompt("You! What is the value of 2 + 2?", "");
if (value == "4")
alert("You must be a genius or something.");
else if (value == "3" || value == "5")
alert("Almost!");
else
alert("You're an embarrassment.");
}
a) The value is initially set to null so that when the while loop checks to verify it should be started, the loop will return true. I.E. value does not equal “4”, it equals null.
You could also set value to anything other than 4.
b)the value you enter into the dialog box will be what value is set to, so since value does not equal 3, 4, or 5 it falls into the all other scenarios with else.