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) Working With Numbers The Random Challenge Solution

My code won't work i cant see why

console.log('Start program');

var firstNumber = prompt('Tell me the fist number'); /* add first number to a variable */
parseInt(firstNumber); /* Convert it into int number */
console.log('first number: ' + firstNumber); /* send a console information */

var secondNumber = prompt('Tell me the second number'); /* add the second number to a variable */
parseInt(secondNumber); /* Convert it into int number */
console.log('second number: ' + secondNumber); /* send a console information */

var randomNumber = Math.floor(Math.random() * (secondNumber - firstNumber + 1)) + firstNumber; /* the random number math, adding it to a variable */

console.log('random number: ' + randomNumber); /* send a console information */

document.write(randomNumber + '!'); /* write the randomNumber on the page */

console.log('End program');

It worked for me in Google Chrome Console.

I swear it is not giving the random number between firstNumber and secondNumber, it really worked fine for you? No changes?

The code itself doesn't give me any errors. You didn't say what specifically you wanted it to do. You want the random number in between first and second? You ordered the code so that the random number comes after first and second. It comes out third for me. A random number every time.

I see, I was not specific, sorry, what I want it to do is to give a random number between firstNumber and secondNumber

Did you try cutting var randomNumber and pasting it in between first and second?

1 Answer

Thanks for the help here is the final version of my code working as I wanted, thanks

console.log('Start program');

var firstNumber = prompt('Tell me the fist number'); /* add first number to a variable */
firstNumber = parseInt(firstNumber); /* Convert it into int number */
console.log('first number: ' + firstNumber); /* send a console information */

var secondNumber = prompt('Tell me the second number'); /* add the second number to a variable */
secondNumber = parseInt(secondNumber); /* Convert it into int number */
console.log('second number: ' + secondNumber); /* send a console information */

var randomNumber = Math.floor(Math.random() * (secondNumber - firstNumber + 1)) + firstNumber; /* the random number math, adding it to a variable */

console.log('random number: ' + randomNumber); /* send a console information */

document.write(randomNumber); /* write the randomNumber on the page */

console.log('End program');

Your new code looks the same as before.

I changed the way I was using de parseInt and it solved the problem somehow, I don't know for sure how it was affecting my code yet