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 trialKimberly Dolcin
4,369 PointsWhat is wrong with my code? This is the challenge: Ready for a challenge? Using Workspaces, create a Random Number Gene
here is my code:
var usernumber = prompt("Enter a number");
alert("your random number is" + Math.random(parseInt(usernumber))+ 1);
2 Answers
Adomas Domeika
6,151 PointsWhen using Math.random, the paranthesis () <- should be empty, like:
Math.random() * parseInt(usernumber) + 1
So if user entered 5, the generator will generate a number between 1 and 5, not including 5. It can even be like 1.6352856281
Antonio De Rose
20,885 PointsNot sure, what is your issue, by the sound, I'd assume, it is a debugging problem
2 options.
option 1 -one right click in a chrome page, click console, clear what ever is inside there, paste your code then hit enter, it will prompt to enter your number, and see, is that what you want.
option 2 - go to https://jsfiddle.net/, paste your code in javascript area, and make sure you run the code, the play button, you will prompt to enter number, and see the result
Kimberly Dolcin
4,369 PointsKimberly Dolcin
4,369 Pointsthank you so much. so to generate a random math number between 1 and six, is this correct: var usernumber = prompt("Enter a number"); alert("your random number is" + Math.random()parseInt(usernumber)+ 1); ?
Adomas Domeika
6,151 PointsAdomas Domeika
6,151 PointsThat's not exactly correct as you wrote it, so if you want to generate a random number between 1 and 6, it would be: Math.random() * 6 + 1; Let me explain that to you more detailed. Math.random() generates a random number between 0 and 0.9999999999 (a lot of nines, not sure how many, but it never reaches 1). So if you want it to generate between 0 and 5.999999 you have to multiply it by 6. If you want to generate between 1 and 6.999999 then you have to add 1 to it. If you only want your answers to be 1, 2, 3, 4, 5 and 6, then you have to round it using Math.floor(). Why not Math.ceil()? - because it can generate 0 and when you add 1 and multiply by 6, then if it generates for example 6.12, it will round to 7, so it is not a solution for you. So, the full correct sentence to generate a random number between 1 and 6 (if you want only rounded numbers(1, 2 and so on)), you will have to write it like this - Math.floor(Math.random() * 6 + 1); If you want your user to enter a number for a maximum number it can generate then it would be:
var usernumber = prompt("Enter a number");
alert(Math.floor(Math.random() * parseInt(usernumber) + 1));
And don't forget to multiply it by user input. ;)
Kimberly Dolcin
4,369 PointsKimberly Dolcin
4,369 Pointsthank you so much for breaking down your answer like that, i wish the teacher wouldve done so. what do you mean by your last line : And don't forget to multiply it by user input. ;) please?
Adomas Domeika
6,151 PointsAdomas Domeika
6,151 PointsYou forgot asterisk -> * <- between Math.random() and parseInt(usernumber)
Kimberly Dolcin
4,369 PointsKimberly Dolcin
4,369 Pointsthanks again Adomas! you are really good at explaining!