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

Why don't my results write to the index.html doc?

On the loops exercise and for the life of me and after an hour of checking, cannot figure out why this code doesn't work. I got it right the first time I thought, then checked against the instructor's explanation code five times. Still no love:

var upper = 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>"); document.write("<p>It took " + attempts + " attempts to guess the correct number.</p>";

Idan Melamed
Idan Melamed
16,285 Points

You might be missing a closing parenthesis on your last document.write

4 Answers

You're just missing a closing parenthesis before your very last semicolon:

document.write("<p>The random number was: " + randomNumber + "<p>"); document.write("<p>It took " + attempts + " attempts to guess the correct number.</p>");

Thanks Niilo, and Idan. I added parentheses...saved, still doesn't work. At a loss here. Will try codepen.io to see if it's something wonky with the learning environment. I did try closing all workspaces and restarting Chrome.

A good way to troubleshoot is to paste the code at codepen.io or in any modern text editor. Most of those tools offer real-time error-checking.

Hmm, maybe it's a workspace issue. The code worked for me when I pasted to Codepen and added the parenthesis.

It's definitely a workspace issue: Sometimes you have to restart Chrome AND click shift to refresh the cache when opening the preview page. Glad it's not me (this time)...thanks guys for the quick responses!! Have a great weekend. -David