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

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge Solution

i think the math is incorrect

hey guys, i did everything pretty much the same way as the instructor. i just named my variables "high" and "low instead of "upper" and "lower". the formulas seems to be correct, but when i input my values the random number that i get back isn't between them,

below is my code: alert("Welcome to the Random number generator game!"); var low = prompt("What is the lowest number?"); var high = prompt("What is the highest number?");

var randomNumber = Math.floor(Math.random() * (high - low + 1)) + low;

alert( "your number " + randomNumber + " is between " + low + " and " + high + ".");

2 Answers

Steven Parker
Steven Parker
231,269 Points

The "prompt" function returns strings. Before performing math on them, you should convert the inputs into numbers. Otherwise, the "+" operator will be interpreted as string concatenation.

The "parseInt" function is handy for this.

Thanks Steven

lucky saito
lucky saito
4,257 Points

try to do it by creating a function like it wants you to do you don't need to do the whole prompt and variables things. the way your doing it is correct expect the parseInt() u need but the exercise is to understand functions.