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 trialMargarita Cascante Makarova
Courses Plus Student 3,053 PointsMy code is not working, can't find the error
Hello everyone! I'm trying to do the challenge, and it seems that I did exactly what the Dave did (except from the message) but the alert shows the same number all the time.
This is my code var input = prompt("Choose a number"); var number = parseInt(input); var randomNumber = Math.floor(Math.random() * number)+1; var message = "This is the number " + randomNumber; alert(message); alert(message); alert(message);
2 Answers
Steven Parker
231,275 PointsYou only compute the number one time, then use it to make the message. When you call alert multiple times, you're only showing that one message over and over.
To see a different number, you would need to perform the calculation again and build a new message using the result.
Justin Cantley
18,068 PointsIf you wrap your code in a function and then call the function several different times you'll get several random numbers. ex) function randomNum() {var input = prompt("Choose a number"); var number = parseInt(input); var randomNumber = Math.floor(Math.random() * number)+1; var message = "This is the number " + randomNumber; alert(message); } Then call the function like so: randomNum();