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
brandonlind2
7,823 PointsI can't find this bug to save my life, please help
You're supposed to guess a random number and if you get it wrong it gives you a second chance with a hint. If I run this code though, it will randomly say the random number is correct or wrong, whether or not you answered the number correctly, also it will sometimes give you a second chance and sometimes it wont.
var correctGuess= false;
/***create random number***/
function randomNumber() {var randomNumber= Math.floor(Math.random() * 10) + 1; return randomNumber;}
/***random number is pulled***/
randomNumber()
/***Question/s are asked and result is displayed***/
var question1= parseInt(prompt("What is the number I'm thinking of?"));
if(question1===randomNumber()) {correctGuess = true}
if (correctGuess === true) {alert("correct, the number was " + randomNumber());}
else if( question1 < randomNumber()) {alert("that is incorrect, try again");
var tryAgain=parseInt(prompt("The number is larger, what is the number?"));
if(tryAgain===randomNumber()) { correctGuess===true; alert("correct, good job, the number was " + randomNumber());}}
else if(question1 > randomNumber()){alert("incorrect, try again");
var tryAgain2= parseInt(prompt("The number is smaller, what is the number"));
if(tryAgain2===randomNumber()){correctGuess=true; alert("correct!, good job, the number was " + randomNumber());}}
else {alert("sorry, the number was " + randomNumber());}
1 Answer
Vittorio Somaschini
33,371 PointsHello Brandon.
The first thing I notice here is that when you run the comparison, like: if(question1===randomNumber()) {correctGuess = true} you are calling the function randomNumber again which means you will get a NEW randomNumber variable. This is not something that you want (if I understood your intensions correctly). I presume you want to only set one variable (which will actually be a constant throughout the code).
Correct me if I am wrong, but I see that you have done this a few times: everytime you run a check you call the function again, resulting into a change of value in the randomNumber variable.
You may want to start correcting this and see if it can lead you to somewhere good.
Let me know
Vitto
brandonlind2
7,823 PointsThanks man you're awesome. There were a few other things wrong but that was one of the major things. Also how did you get the ```javascript ticks to work, I was having trouble with that for some reason, what did you change?
Vittorio Somaschini
33,371 PointsHello Brandon,
glad to hear you sorted that one and the rest out! ;)
For the code formatting part: You wrote JavaScript instead of javascript I simply changed those 2 letters.
If you need to know more in the future, you can have a look here: https://teamtreehouse.com/forum/posting-code-to-the-forum
Have a happy day of coding!
Vitto
brandonlind2
7,823 Pointsok cool, thanks for the help!
Vittorio Somaschini
33,371 PointsVittorio Somaschini
33,371 PointsHey Brandon, I edited your question just for readability.