Javascript tutorial, please explain
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.
The value != 4
is the condition for the while
loop to process. As long as value
does not equal (!=
) 4, then it will continue prompting.
Once inside the while loop, it gets you to enter a number.
Then the number is checked (the if-else if-else
) and does line following beneath it. If something other than 4 is entered, such as 2, value
does not equal 4 and will prompt you to enter a number again.
This will continue until you enter 4.
I've added some comments (and braces) to the code sample, hopefully it will help clarify things...
var value = null;
while (value != "4") {//While value is not 4 do the following code
value = prompt("You! What is the value of 2 + 2?", "");//Get a value from the user
if (value == "4"){//If value equals 4, then do the following
alert("You must be a genius or something.");
}//end of if block
else if (value == "3" || value == "5"){//If value is equal to 3 or 5 then d0
alert("Almost!");
}//end of else-if block
else{//If value is something we haven't explicitly checked for, then do this
alert("You're an embarrassment.");
}//end of else block
}//End of while loop
EDIT - My response to the comment was too long
This block of code works as follows:
var value = null
Create a variable named value and set it equal to null
while(value != 4){...whileblock...}
(ignoring the code inside the while loop) While value is not equal to 4, then execute the whileblock
code
Once we are inside the while loop (which will happen since null
does not equal 4
) we start operating on the additional code
value = prompt("...");
Ask the user to input something
if(value=="4"){...ifblock...}
else if(value=="3" || value=="5"){...ifelseblock...}
else{...elseblock...}
This block operates as: If value is equal to 4, do the ifblock
of code. If my value is not equal to 4, then check the next component, which is if(value=="3"||value=="5")
. This says, If my value is 3, or if my value is 5, then do the code in ifelseblock
. If we didn't do ifelseblock
then go to the next component, which is the else
part. That says, whatever is in value doesn't matter, we made it this far down, so go ahead and run elseblock
. (Of course, replaced each of those *block
pieces with the corresponding code)
Now that it is done, the while loop returns to the top to see if we should continue executing the whileblock
code. So it checks to see if value does not equal 4. If value equals 4, it won't run the loop again. If value is something other than 4, it will run all, that code again, asking for a prompt, doing the comparisons and displaying the appropriate alert box.
I hope all that helps clear it up some.
Well if you type in 4, it will say "You must be a genius or something.". If you type 3 or 5, it will say "Almost!" Anything else it will say "You're an embarrassment"
精彩评论