Jump to content

I am needing help with this while loop


nhuyvan1106

Recommended Posts

    function getPassword(){      var correct = "HTML5";      var guess = "";      while (guess != correct){        guess = prompt("Password?");      } // end while      alert("You may proceed");    } 

Hey guys, I am a newbie, here, so it would be much appreciated if you guys could clarify this issue for me. As you can see the above javascript function (asking the user to enter the correct password after they click on the button or the loop will continue forever), my question is that, why do we need to set the "guess" variable's value to empty in this case, why don't we just leave it as undefined(var guess;), I've seen other people's codes like that as well, they always set the initialization variable's value to empty. The second question is, why do we have "guess = prompt('Password?')", is it okay if I just write "prompt = ('Password?')". Please help me guys, any suggestions are greatly appreciated. God bless you.

Link to comment
Share on other sites

guess is probably initialized to a string so that it doesn't have to be casted to a string in the first comparison. It's nothing really important

 

prompt() is a function belonging to the Window object: http://www.w3schools.com/jsref/met_win_prompt.asp

It returns the value that the user put into the prompt, which is assigned to the guess variable.

 

prompt = ('Password?') wouldn't work, it would just assign the string "Password?" to a variable prompt (overwriting the original function prompt() in the process)

  • Like 1
Link to comment
Share on other sites

If you do, then the prompt will appear, but you won't know what the user wrote. The value that the user gives is stored in the variable you assign prompt() to.

  • Like 1
Link to comment
Share on other sites

Man, that makes alot of sense. It is hard sometimes when you are teaching yourself something, Im glad I found this forum and website. Thank you very very much Foxy mod, you don't know how much I appreciate the help, god bless you. Cheers

Link to comment
Share on other sites

 

 

why do we need to set the "guess" variable's value to empty in this case...

 

In JavaScript it is merely a good practice to get used to. It improves readability of your code when others come back to look at it. JavaScript is mostly type-insensitive and also automatically makes sure that new variables made are truly empty if never initialized. There is no direct type classification when making a variable and JavaScript merely assumes the type by what is currently entered in it, casting (changing) the data to other datatypes when needed.

 

In other languages, like C, the language will assign some random available address to the variable and immediately use whatever is already in that address (usually just garbage data). You're required to determine the datatype at creation so that the language knows how many bits the variable needs to use. If you forget to initialize variables in those languages your code could run away with non-sensible info making it do bad things.

 

So if you plan to write in multiple languages always initializing is simply a good habit to get into, unless you have some special use case in explicitly NOT initializing.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

 

In JavaScript it is merely a good practice to get used to. It improves readability of your code when others come back to look at it. JavaScript is mostly type-insensitive and also automatically makes sure that new variables made are truly empty if never initialized. There is no direct type classification when making a variable and JavaScript merely assumes the type by what is currently entered in it, casting (changing) the data to other datatypes when needed.

 

In other languages, like C, the language will assign some random available address to the variable and immediately use whatever is already in that address (usually just garbage data). You're required to determine the datatype at creation so that the language knows how many bits the variable needs to use. If you forget to initialize variables in those languages your code could run away with non-sensible info making it do bad things.

 

So if you plan to write in multiple languages always initializing is simply a good habit to get into, unless you have some special use case in explicitly NOT initializing.

Thanks alot bro

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...