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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

My code isn't working...can't see the error

My code is below...yet nothing is showing up....what I have I done wrong?

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 correct number was " + randomNumber ".</p>");
document.write ("<p>The computer guessed " + attempts " times.</p>");

2 Answers

Tobias Helmrich
Tobias Helmrich
31,601 Points

Hey there Dave,

your code works just fine for me! I pasted it into Codepen here and it works there! If you're using Workspaces you might want to restart it.

I hope that helps! :)

Hi there Tobias,

Thanks for the reply. I can see it works in your code pen and I have tried reseting it it still isn't working. Very odd and frustrating. I am having an issue with another code in workspaces, so maybe it is just workspaces. I have no idea what is happening but it seems I am spending all day trying to fix code that doesn't need to be fixed just because workspaces isn't showing it. Odd.

Tobias - when I run the console it says on line 15 - Uncaught SyntaxError: missing ) after argument list ...I have no idea what this means or why it is not showing up...

I finally worked it out....I need to add a + to my document.write statements correction was...

document.write( '<p>The correct number was ' + randomNumber + '.</p>' );
document.write( '<p>The computer guessed ' + attempts + ' times.</p>' );

Hi Kiwi,

In your document.write statements you're missing + signs after the variable names to complete the string concatenation.

It seems that codepen is correcting this and modifying the script instead of causing an error.

If you search for your script inside the iframe that codepen uses, you should find:

document.write('<p>The correct number was ' + randomNumber, '.</p>');
document.write('<p>The computer guessed ' + attempts, ' times.</p>');

So it placed a comma where you had intended to put +. It's effectively 2 arguments now which will be concatenated the same as if a plus sign was there.

I'm not sure why it does this though and it doesn't seem to be a good thing to do.

I see you figured it out already.

Yeah, thanks Jason. Took me forever and was very frustrating - I almost broke my Mac in the process :)

Tobias Helmrich
Tobias Helmrich
31,601 Points

Ah, I'm sorry, I completely missed that! After it worked immediately in Codepen I thought everything was fine and Workspaces is causing the issue. Good spot Dave and Jason, it really seems that Codepen is fixing this automatically!

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,304 Points

I deliberately did not read the comments, was testing myself if I could pick up the error, and I spotted it on my second reading of the entire code, the two missing '+' symbols. I must be improving lol. These things are tough to spot if you are still starting on code and JavaScript. Happy coding guys!