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 trialNishoo Bansal
Front End Web Development Techdegree Student 123 PointsMy java error ?
var randomnumber = getrandomrumber (10) ; var guess ; var guesscount = 0 ; var correctguess = false; function getrandomrumber(Upper) { var num = Math.floor (Math.random () * upper) + 1; return num; }
do { guess = prompt ("Guess what I am thinking"); guesscount += 1; if (parseint(guess) === randomnumber) { correctguess = true } } while ( ! correctguess )
alert ("You took" + guesscount + "guesses to think of correct number")
This is my code for do while lesson but it is not working can anyone tell me error.
1 Answer
Katie Wood
19,141 PointsHello there,
First, I'm going to format the code you typed so it's easier to read, and so that you know what I started with:
var randomnumber = getrandomrumber (10) ;
var guess ;
var guesscount = 0 ;
var correctguess = false;
function getrandomrumber(Upper) {
var num = Math.floor (Math.random () * upper) + 1;
return num;
}
do {
guess = prompt ("Guess what I am thinking");
guesscount += 1;
if (parseint(guess) === randomnumber) {
correctguess = true
}
} while ( ! correctguess )
alert ("You took" + guesscount + "guesses to think of correct number")
Here are the things causing errors:
- On the getrandomrumber function, the parameter is Upper, with a capital U, but it is referred to as 'upper' later - these need to match.
- in your do-while loop, parseint() needs to be parseInt() (capital i)
At this point it should work, though I do have a couple of things to point out:
- It might be worth considering using camel casing for your variable and function names - for example, getRandomNumber() can be easier to read with all the letters mashed together. Ultimately, you can do whatever you want, but using typical convention can make it easier for you to read, and make it easier on any teams you might code with in the future.
- The final output (number of guesses to get correct number) does appear once the errors are fixed, but it's missing some spaces - you can fix this by adding spaces on either side of the variable like this:
alert ("You took " + guessCount + " guesses to think of correct number")
// here ^ and here ^
Hope this helps!
Nishoo Bansal
Front End Web Development Techdegree Student 123 PointsNishoo Bansal
Front End Web Development Techdegree Student 123 PointsThanks for the answer