nhuyvan1106 Posted May 4, 2014 Share Posted May 4, 2014 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 More sharing options...
Ingolme Posted May 4, 2014 Share Posted May 4, 2014 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) 1 Link to comment Share on other sites More sharing options...
nhuyvan1106 Posted May 4, 2014 Author Share Posted May 4, 2014 Oh I'm sorry, I mean "prompt('Password?')"( shouldn't have had the "=" sign). Can I write "prompt('Password?") only? Thank you very much Foxy mod. Link to comment Share on other sites More sharing options...
Ingolme Posted May 4, 2014 Share Posted May 4, 2014 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. 1 Link to comment Share on other sites More sharing options...
nhuyvan1106 Posted May 5, 2014 Author Share Posted May 5, 2014 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 More sharing options...
Hadien Posted May 5, 2014 Share Posted May 5, 2014 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. 1 Link to comment Share on other sites More sharing options...
nhuyvan1106 Posted May 18, 2014 Author Share Posted May 18, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now