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 trialTall Toad
2,746 PointsWhy is console.log() not having an issue showing numbers without the parseInt?
// Collect input from a user
const choosenNumber = prompt("Whats your favourite number?");
// Convert the input to a number const favouriteNumber = (choosenNumber);// I know parseInt should be present before the (chosenNumber). But when I run this workspace on the browser, I'm not getting any errors or Nan.
// Use Math.random() and the user's number to generate a random number
const randomNumber = Math.floor( Math.random() * choosenNumber) + 1;
// Create a message displaying the random number
console.log(${randomNumber} is a number between 1 and ${favouriteNumber}.
);
1 Answer
Steven Parker
231,236 PointsFor some operations, JavaScript can tell that a number is needed and it performs type coercion, which is essentially converting the string into a number for you automatically.
But you don't want to rely on this, since there will be other cases where it can't perform this for you. Here's an example where it won't work:
const choosenNumber = prompt("Whats your favourite number?");
const twice = choosenNumber + choosenNumber;
console.log(`${choosenNumber} added to itself is not ${twice}, is it?`);
Tall Toad
2,746 PointsTall Toad
2,746 PointsThanks for the reply Steven Parker, I'm new to coding. Some parts make sense some don't or eventually do. As a newbie, How does one evaluate if they are doing well or not?
Steven Parker
231,236 PointsSteven Parker
231,236 PointsIf you generally complete the quizzes and challenges with only occasionally needing to review a lesson, you're doing quite well indeed!