Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kandance Ferguson
6,271 PointsJavascript Loops
I think I am making this more difficult than it really is :( I am a little confused when it comes to while loops and do while loops. I am not sure when to fully define the variable before the loop and reference it inside the loop OR when to just state the variable name and define it inside the loop. For example:
While completing a code challenge that dealt with while loops, I had to define the variable before the loop and then reference it again inside the loop like so:
var secret = prompt("What is the secret password?");
while (secret !== "sesame"){
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
Why do I have to retype what secret means inside of the loop? Why not just reference the variable name?
For a do/while loop, again while completing a code challenge, I typed:
var secret;
do {
secret = prompt ("What is the secret password?");
} while (secret !== "sesame")
document.write("You know the secret password. Welcome.");
Why don't I have to fully define the variable before the do/while block? Why only type it in the code block?
HELP!!
Thanks :)
4 Answers
Micah Kline
17,831 PointsWhen you declare the var outside of the loop it is created only once. If you create it inside the do loop it is created every time it loops. Can create overhead and more work for the processor.
Jesus Mendoza
23,289 PointsI believe you can declare it inside or outside the loop in either the do/while or the while as long as you give the variable a value inside the loop or it will crash. For example:
var secret = prompt("What is the secret password?");
while (secret !== "sesame"){
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
Can be the same as:
var secret;
while (secret !== "sesame"){
secret = prompt("What is the secret password?");
}
document.write("You know the secret password. Welcome.");
In this case you need to give a value to the var secret inside the while or it will crash
I'm not really sure thought
LaVaughn Haynes
12,397 PointsJesus is correct in that you could technically do it either inside or outside. You only need to define the variable once though. It would still work if you only defined it inside of the do-while loop but as Micah said that would be inefficient to define it repeatedly. Treehouse could make you re-register EVERY TIME you come to the website but that would similarly be inefficient. Makes more sense to register once and then reference that initial registration every time you come back.
With the while loop you could also only define the variable inside of the loop but you're not only going to have the same inefficiency as with the do-while loop, you are also making the code do unnecessary work by testing the variable before you've created a condition in which it could even pass the test. I guess my metaphor for that would be if Treehouse gave you the code challenge before showing you the video lesson. Then you'd fail and have to do it again anyway. Sorry for the metaphors :)
LaVaughn Haynes
12,397 PointsTwo more quick things...
1) When I said that you could technically define that variable inside or outside of the loop, I was only talking about in this example with the prompt, not as a rule. For example, if you did this it would create an infinite loop and crash your program:
while (counter !== 10){
var counter = 0;
console.log(counter);
counter = counter + 1;
}
The variable counter would never get to 10 because in every iteration of the loop you reset it back to 0.
2) Finally, in JavaScript only functions have scope so anything that you define in a loop is still going to be accessible outside of the loop anyway. It's probably better to just always define all of your variables at the top of the script or the top of a function as the Treehouse instructors suggest.
Kandance Ferguson
6,271 PointsThanks all!
Micah Kline
17,831 PointsMicah Kline
17,831 PointsMaybe I misread your initial question. In your own program you should be able to declare the var and the ask the question once in the loop. What if you only want the code in the while loop to run after they got it wrong. If you don't prompt before the while loop it runs as does all the code in the loop and since prompt is a method call you have to assign it every time you wish to prompt. The do loop will run at least once. So prompting before and after would cause two prompts.