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 trialCarlo Sabogal
7,468 Pointsquestion about a simpler do while loop and why doesnt work.
I don't understand why the program cannot exit the loop on my shorter version of his example. In my code, shouldn't the "guess" variable at some point actually guess the randomNumber and allow the program to exit the loop? When I run this code, I get an infinite loop. Please explain because I don't understand why an exit is needed if at some point guess will be = to randomNumber. thanks!
...javascript
var upper = 10; var guess; var randomNumber = getRandomNumber(upper);
function getRandomNumber (upperlimit) { return Math.floor(Math.random() * upperlimit)+1; }
do { guess = prompt("guess the number"); } while (guess!==randomNumber)
....
Carlo Sabogal
7,468 PointsFlavio,
If you watch the video he places the variable before the function.
About upper vs upperlimit, it works either way. The code is failing by creating an infinite loop.
thanks
carlo
Flavio Carvalho
26,636 PointsHi Carlo,
I still don't think the 'upperlimit' is giving you anything, considering you're assigning your value of 10 to upper. If you see, in the video he assigns 10 right into the parameter when creating the variables. So your '10' value is never being placed on your function.
More specific to your question though, your do while loop is not really returning true to your condition. For one, you need to 'parseInt' the response from the user, the 'guess' because in principle that is a string and your random number is a float... in other words, without parsing your string your condition will always be false (comparing string vs float), thus you get an infinite loop.
Try parsing your 'guess' and then test it. Also, using a flag variable like he does in the video is just more logical to the code.
Let us know if you figure something!
1 Answer
Jamie Perlmutter
10,955 PointsIt might be because you guess has not been changed into an integer, ... parseInt(guess) !== randomNumber... should fix the problem. the prompt would bring back a string, if you convert the string into an integer then it should allow you to guess one of the numbers. The randomNumber would be an integer so if you compare the integer to the string the prompt brought in, it would never equal. However if once its converted to an integer, you can compare and the loop can end
Jamie Perlmutter
10,955 PointsI said it might, it is because guess has not been changed. I ran it through with that change and it was able to exit once the correct number was put in
Flavio Carvalho
26,636 PointsYeap... exactly what I mentioned about the parsing :)
It worked right! Comparing string and integer will return false.
Flavio Carvalho
26,636 PointsFlavio Carvalho
26,636 PointsHi Carlo,
I believe you 'function getRandomNumber(upper)' needs to be at the top before you create and assign its value to the variable 'randomNumber. Basically, getRandomNumber does not exist when the code is read where the variable is, thus there is no value being passed onto it.
Another little thing that I think is: is it upper or upperlimit? You are declaring var upper, but your passing the parameter upperlimit.
I could be mistaken, but try those and let us know if it helps! :)