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
Omar Bahaa
Courses Plus Student 1,801 PointsWrong output!
The output is over the first input and not between 0 and inputOneNum, i believe it's something with the document.write syntax which i did wrong.
My code below:
var inputOne = prompt('Add your first input');
var inputOneNum = parseInt(inputOne);
//var inputTwo = prompt('Add your second input');
//var inputTwoNum = parseInt(inputTwo);
document.write('Your random number from 0 to ' + inputOneNum + ' is ' + Math.floor(Math.random() * inputOneNum) +1);
2 Answers
Linas Mackonis
7,071 PointsActually I wrote the parseInt into one variable, so my full code looks like this:
var inputOne = parseInt(prompt('Add your first input'));
document.write('Your random number from 0 to ' + inputOne + ' is ' + Math.floor(Math.random()*inputOne+1));
Linas Mackonis
7,071 PointsIt's about putting '+1' not in the right parentheses. Set +1 like this:
document.write('Your random number from 0 to ' + inputOne + ' is ' + Math.floor(Math.random()*inputOne+1));
and you will get the number between 0 and inputOne
Omar Bahaa
Courses Plus Student 1,801 PointsOmar Bahaa
Courses Plus Student 1,801 PointsThank you Linas, it appears i have a long way to be more comfortable with JavaScript.