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

Liam Greig
Liam Greig
2,233 Points

Ouff. What am I missing here?

I feel like I must be missing something obvious here. The code for this video makes sense to me but I can't get it to run without crashing my browser. What am I missing? Thanks!

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('I am thinking of a number between 1 and 10. What is it?');
  guessCount += 1;
  if (parseInt(guess) === randomNumber){
    correctGuess = true;
  }
} while ( ! correctGuess )

document.write('You got it!');

5 Answers

Liam Greig
Liam Greig
2,233 Points

Jennifer Nordell , Steven Parker soooo... turns out I'm an idiot. I was guessing the same number over and over each time - totally gapped on the fact that the randomNumber is set once and doesn't change with each guess ?

Oops. Thanks for your help. Onwards!! ?

Steven Parker
Steven Parker
231,269 Points

But isn't the whole point that if you guess a different number each time you'll eventually get the right one? And I still don't get why the browser would become unresponsive after a while.

Steven Parker
Steven Parker
231,269 Points

This code looks good to me. But be aware that it will continue to demand input until you enter the correct number. Is that what you were calling "crashing"?

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! To be honest, I don't think you're missing anything. Your code runs just fine in my browser.

This makes me a little suspicious about your browser. In the front-end courses workspaces is essentially just a convenient text editor, but the code is executed on your machine in your browser. There is the off chance that what is being loaded in is not the code listed here, but rather a cached version of your code, which may have contained some error that was causing an infinite loop. I'm curious what happens if you clear the browser cache and start the preview and refresh the page. If this doesn't work, what browser and version are you using?

Looking forward to your response! :sparkles:

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I am assuming you are starting from 1 and working all the way up to 10? That's what I do and eventually it will be correct.

Liam Greig
Liam Greig
2,233 Points

Yeah, something odd is going on. I've cleared my cache and tried running the same program in FF and still getting the same response - I'm prompted to guess a number but my 'guess' is always wrong. Eventually the browser becomes unresponsive and I have to quit and start again...

Steven Parker
Steven Parker
231,269 Points

Try a different browser? I rarely have any trouble using Chrome.

Liam Greig
Liam Greig
2,233 Points

I think I was just getting unlucky. When the program runs, it sets the random number between 1 and 10 - let's say it was set to 8. Then I'd just keep guessing 1 over and over so it was never the correct guess. I must have tried that a couple of dozen times without ever getting it right.

I'm not sure why the browser was becoming unresponsive either though. Maybe because the program was still trying to run the do ... while loop? A bit odd for sure...

Steven Parker
Steven Parker
231,269 Points

That would be guessing the same number each time, and yes, that would explain the program never ending :smirk: (but not the unresponsiveness).