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 trialAlfredo Garcia
Full Stack JavaScript Techdegree Student 6,098 PointsparseInt(firstNumber); vs var convertedMin = parseInt(firstNumber);
Hello,
So I've been following this track every step of the way. I've come into a stumbling block with my code, and although I found the solution, I'd like to understand what is happening.
Here is my code:
var firstNumber = prompt('What is the first number?');
var secondNumber = prompt('Oh great! You picked, ' + firstNumber + '. Which is the second number?');
parseInt(firstNumber);
parseInt(secondNumber);
This didn't kept giving me a result in the thousands.
However, changing it to this:
var firstNumber = prompt('What is the first number?');
var secondNumber = prompt('Oh great! You picked, ' + firstNumber + '. Which is the second number?');
var convertedMin = parseInt(firstNumber);
var convertedMax = parseInt(secondNumber);
Made the random generator work flawlessly.....
I'm just a tad confused as to what is going on. For reference my code is below:
// Let's announce the challenge.
alert('Let\'s play a game. Give me two random numbers, and I\'ll pick one between those two numbers. Sounds good?');
//Collect the numbers from the player.
var firstNumber = prompt('What is the first number?');
var secondNumber = prompt('Oh great! You picked, ' + firstNumber + '. Which is the second number?');
// Let's convert these numbers.
var convertedMin = parseInt(firstNumber);
var convertedMax = parseInt(secondNumber);
// Communication!
alert('Hmm....a number between ' + firstNumber + ' and ' + secondNumber + '.');
// Let's do the math.
var finalAnswer = Math.floor(Math.random() * (convertedMax - convertedMin + 1)) + convertedMin;
// Lights, camera, ACTION!
document.write('<h2>A number between ' + firstNumber + ' and ' + secondNumber + ' is ' + finalAnswer + '.</h2>');
1 Answer
Steven Parker
231,275 PointsYour comments seem to indicate you already understand the program.
The only potential area of confusion I see is in the very first example, where you call parseInt two times, but do not store the result anywhere. While this may not cause an error, it doesn't do anything useful.
In the other examples, you correct this and save the numeric results of the parseInt functions so they can be used later in the program.
Alfredo Garcia
Full Stack JavaScript Techdegree Student 6,098 PointsAlfredo Garcia
Full Stack JavaScript Techdegree Student 6,098 PointsThank you for explaining. I knew the problem and the solution, just wanted to know where I went wrong in the first example.
Thanks!