Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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