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 trialKiwi Dave
5,541 PointsMy 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
31,603 PointsHey 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! :)
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 PointsI 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!
Kiwi Dave
5,541 PointsKiwi Dave
5,541 PointsHi 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.
Kiwi Dave
5,541 PointsKiwi Dave
5,541 PointsTobias - 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...
Kiwi Dave
5,541 PointsKiwi Dave
5,541 PointsI finally worked it out....I need to add a + to my document.write statements correction was...
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi 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:
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.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsI see you figured it out already.
Kiwi Dave
5,541 PointsKiwi Dave
5,541 PointsYeah, thanks Jason. Took me forever and was very frustrating - I almost broke my Mac in the process :)
Tobias Helmrich
31,603 PointsTobias Helmrich
31,603 PointsAh, 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!