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 Review Numbers and the Math Object

What will appear in the JavaScript console after this code runs? console.log( parseInt( '7 days a week' ) );

Hi all, I mustn't be understanding something about parseInt. While doing the quiz the question of what will this code: console.log( parseInt( '7 days a week' ) ); return? I said NaN since there's quite a bit of this string that isn't a number. Why is the answer then 7? Does parseInt ignore all the other parts of the string? (aka: 'days of the week') If the parseInt does just ignore the letters, why? I am not understanding why the result of the parseInt on '7 days a week' isn't either transforming all of the string, including the alphabet characters into mythical integers or why this doesn't create an error since alphabet characters aren't integers.

Note: I do understand from another part of the quiz that the information in the parens (is the information contained in the parens called an argument?) must start with a number. I also am a beginner, please post responses at a beginner's level of understanding.

Thanks for your help!

1 Answer

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Abbyman,

Your understanding of parseInt() is nearly complete.

The function will take strings with numeric data and change the string to an integer. So, if you start with a string containing the number 777, if you wanted to convert that string to an integer in order to use it as a number instead of a line of text, you would use parseInt().

As for the confusing part, parseInt() requires the string to begin with a numeric value, otherwise it will return NaN (not a number).

So, if you have the string '7seven' and use parseInt('7seven'), the returned value will be the integer 7.

If you begin with the string 'seven7', and use parseInt('seven7'), the returned value will be NaN.

So yes, the string must begin with a number and it will cut off everything after the number the second any character that isn't a number appears.

I guess I should add an example of why we would want this functionality at all.

Let's use the hypothetical event that we have a database that tracks the high scores of players for a game. For whatever reason, the scores are entered into the database and stored as strings. If we tried to pull these strings from the database and wanted to multiply the scores by two for a bonus event, we wouldn't be able to do it without creating a pretty complex solution if parseInt() didn't exist since we can't multiply string information.

Thankfully, parseInt() allows us to turn the string values into integers, and then we can multiply them easily.

Sure, the we could just change the column type to integer in the database, but that would assume we had access to change the hypothetical database, which isn't always the case in real world application.

Good luck with the course!