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 trialCrystal Scott
5,249 PointsMy code only ran correctly if I added my inputs not subtracted as the video instructed, why is this?
// Collect input from a user let input1 = prompt("What is your lowest number?"); let input2 = prompt("What is your highest number?");
// Convert the input to a number input1 = parseInt(input1); input2 = parseInt(input2);
if (input1 && input2){ // Use Math.random() and the user's number to generate a random number const randomNum = Math.floor(Math.random() * (input1 + input2 + 1)) + input1; /* THE VIDEO SHOWED: const randomNum = Math.floor(Math.random() * (input1 - input2 + 1)) + input1; but this did not work for me until I changed the subtraction to addition. Help! */
// Create a message displaying the random number
const main = document.querySelector('main');
main.innerHTML = Your random number between ${input1} and ${input2} is ${randomNum}!
;
}else{
alert("Please refresh your browser and enter two numbers!");
}
1 Answer
Joseph Yhu
PHP Development Techdegree Graduate 48,638 PointsLook at your code again; you are subtracting the higher number of the two from the lower number. Therefore, this part of the code: input1 - input2 + 1
will always be less than or equal to 1, assuming the two numbers can be equal.
Edit: Changing the minus to plus won't always work either since the result can get outside of the range (i.e. greater than the max).