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 A Closer Look at Loop Conditions

Robert Kooyman
Robert Kooyman
4,927 Points

Why is my Code not displaying anything?

This is my code. I have no clue what I'm doing wrong here? Any help would be great! :)

var uppper = 10000;
var randomNumber = getRandomNumber(upper); 
var guess;
var attempts = 0;


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

while (guess !== randomNumber) {
  guess = getRandomNumber( upper );
  attempts +=1;
}
document.write("<p>The random number was: " + randomNumber + "</p>");
docment.write("<p>It took the computer" + attempts + " attempts to get it right.</p>");

3 Answers

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

Hey, when you declare your var upper in the beginning, you spell it with 3 pĀ“s (uPPPer), while later on, you refer to it as uPPer. You also misspelled document.write towards the end.

If something doesnt work, the best thing to do first is to open the Javascript Console, (for chrome: Use the keyboard shortcut Command + Option + J (Mac) or Control + Shift + J (Windows/Linux). Select > More Tools > JavaScript Console .)

Really often our code doesnt work because of simple typos. Happy coding :)

I notice three errors in the code you posted:

  1. You are trying to call a function that is not yet defined in line 2. This will result in an "Uncaught ReferenceError". So, define the function getRandomNumber(...) at the very beginning of the script before variable declarations.
  2. There's a typo in variable name 'uppper'; remove the extraneous 'p'.
  3. 'docment' is referenced rather than 'document'; substitute the missing 'u'.

Doing the above changes should make your code work.

It's just a few spelling mistakes on the variable 'upper' and the document object.

You tried to parse a variable upper into the getRandomNumber() function, but because upper is not defined (uppper is defined) it won't generate a number since it has no range to generate one from.