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

Leonardo Motta
Leonardo Motta
11,284 Points

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');
Mike Hatch
Mike Hatch
14,940 Points

It worked for me in Google Chrome Console.

Leonardo Motta
Leonardo Motta
11,284 Points

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

Mike Hatch
Mike Hatch
14,940 Points

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.

Leonardo Motta
Leonardo Motta
11,284 Points

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

Mike Hatch
Mike Hatch
14,940 Points

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

1 Answer

Leonardo Motta
Leonardo Motta
11,284 Points

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');
Mike Hatch
Mike Hatch
14,940 Points

Your new code looks the same as before.

Leonardo Motta
Leonardo Motta
11,284 Points

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