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

Francesca Haines
Francesca Haines
1,890 Points

My Random Challenge (JS Basics) code works, but is quite different to the answer.

Instead of updating the user input variable using the parseInt method, I updated the current variable (or at least I think I have!) like so:

var userNumber = prompt("Give us a random number!"); 
parseInt(userNumber); 
var computerNumber = Math.floor(Math.random() * userNumber) + 1;

document.write(computerNumber);

Dave's code creates a new variable using the parseInt method. Is my solution actually incorrect, or just bad practice? It actually works fine when I run it.

fixed code formatting

1 Answer

andren
andren
28,558 Points

I updated the current variable (or at least I think I have!)...

I can certainly see why you would think that, but that is actually incorrect. The parseInt function takes a string and returns an int version of it. It does not modify the variable you call it on.

This means that in order to use the result you have to assign it to something (or use it in a location that is expecting an int), currently you do nothing with the returned value. Meaning that you could in fact remove the second line from your program and it would have zero effect on its functionality.

The reason why your code works anyway is that JavaScript always tries to convert values to whatever type they need to be for certain operations they are involved in. When you multiply a string and a number together (which is what you end up doing in your code) then the string will automatically be converted to a number, due to the fact that multiplication cannot be performed on a string.

I would classify this as a bad practice though, since you should never store a pure number as a string. The reason for this is that while JavaScript will convert it to a number automatically for certain operations it won't do so if there is some ambiguity. A good example of this is the + operator. + can be used on both strings and ints but it has different meanings. With a string + is used for concatenation, meaning that it simply glues the strings together.

If you combine a string and a number using + then both will be treated as strings and they will be concatenated instead of added together like you'd expect. So "5" + 2 for example will actually result in "52" ("5" and "2" glued together) not 7 (5 + 2) like you'd expect.

So while not using parseInt works in this specific instance it will lead to buggy and incorrect behavior in many other cases, so it's definitively not a good practice.

You don't have to create a different variable to store the value in like is done in the video, you can store it back into the same variable like this:

userNumber = parseInt(userNumber); 

But you do have to do something with the value parseInt returns, simply calling it on it's own like you have done in your code won't do you any good.

That explanation ended up being way longer than I intended so I apologize for that, but hopefully it was informative for you. If you have any further question I would be glad to answer them.

Francesca Haines
Francesca Haines
1,890 Points

Hi Andren,

This makes complete sense and thank you so much for explaining it in so much detail. Definitely learnt a lot here!

Fran