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 trialChaya Feigelstock
2,429 PointsWhy is the integer that is passed through "age" automatically converted into a string? What is the logic behind it?
Craig briefly mentioned when going through the code at the end that the integer passed through "age" is automatically converted into a string but didn't explain why?
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Chaya Feigelstock ! This is a really common question and you're far from the first to get tripped up by this. In fact, it's so common because it applies to literally every programming language I've dealt with including Swift, C#, Python, Ruby, JavaScript and others. Whenver you send a prompt to the user, whatever they type in comes in as a string. This is true for programming languages just across the board (as far as I know and definitely those listed above). But there are numbers that are ok to have as strings, too. For example, a zip code would be a great number to have as a string. You don't need to do any math on it, or a telephone number. Same thing there.
But here, we need the age to be a number we can do math on so we have to tell it to take what the user sends in (which, again, is a string) and turn it into an integer, which is a number we can perform math on later.
And think about it for a minute. Most of the time when we're asking a user for information, we're really wanting a string (even when it's a number). We might ask for their name, address, zipcode, state, email address and more. While some of those may contain numbers, the numbers we actually need to use math on are relatively limited so it makes sense to have the information coming in from the user a string by default and then change it to a number if we need it that way
Hope this helps!