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

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

Ending up with endless loop although I literally typed each and every letter like teacher on the video. What can it be?

I really got very frustrated because I keep getting endless loop and then the page crashes every time so I have to close and open browser again and again. Lost all day on this issue. I checked each and every letter and sign 50 times and surely I am not making any typo. Is this something that happens often, or has anyone had a similar experience?

Thanks for any replies and thanks to the Treehouse for great course!

Hi Aleksander,

Do you happen to have you code available for me to take a look?

Thanks, Chris

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

Thanks Christopher.

I would certainly love to share it with you, but I do not know how exactly to do that since this is the first time I am asking a question here at Treehouse. Any tips on that as well :-) ?

Haha, This is my first time answering a question. Are you doing this in workspaces? If so you should be able to copy it from there and put it in here. you can also wrap it in three ( ''' )

-Chris

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

Haha, well then let's brake two ice! Here is the code!

var randomNumber = getRandomNumber(10); var guess ; var guessCount = 0; var correctGuess = false ;

function getRandomNumber(upper) { var num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt("Please type a number beetween 1 and 10 and try to match my random number, which is inside this range."); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while ( !correctGuess )

document.write('Well done! I was thinking of ' + randomNumber + ' and it took you ' + guessCount + ' to guess it!');

Its okay, Think I got everything.

3 Answers

Just for the record, it was probably because you had parseInt===randomNumber instead of parseInt(guess)===randomNumber. If you forget to insert that it will prompt you but won't do anything with your guess so it will never end.

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

What do you think Christopher? I really need to resolve this before I get to bed , otherwise I won't be able to sleep knowing myself :-)

Hey Aleksander,

I ran this code

var randomNumber = getRandomNumber(10); 
var guess; 
var guessCount = 0; 
var correctGuess = false ;

function getRandomNumber(upper) { 
    var num = Math.floor(Math.random() * upper) + 1; 
    return num; 
}

do { 
    guess = prompt("Please type a number beetween 1 and 10 and try to match my random number, which is inside this range."); 
    guessCount += 1; 
    if (parseInt(guess) === randomNumber) { 
        correctGuess = true; 
    } 
} while ( !correctGuess )
    document.write('Well done! I was thinking of ' + randomNumber + ' and it took you ' + guessCount + ' to guess it!');

and it worked fine. you just have to guess the right number.

Good Job!

Aleksandar Stankovic
seal-mask
.a{fill-rule:evenodd;}techdegree
Aleksandar Stankovic
Python Development Techdegree Student 9,648 Points

I am also thinking it is a good code. Trust me, I tried some 5 minutes, typed some 100 numbers but could not guess it! It seems it is something with my browser than.

Anyway that is a relief.

Thanks Christopher on your help, thanks a lot!