Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Brian Prouty1,792 Points
The code will only work if I take out 'parseInt()' command...
Please ignore my newbie comments integrated into my code lol... but I found that my code will only work if I take the "response = parseInt(response);" As soon as I take it out it works fine but I don't want to run into future problems with the code...
parseInt method takes a response and turns it into an
int, which is the datatype used to represent numbers. This is a good practice to do if you are working with numbers but it can cause issues if:
- The answer is not stored as an
intbut as a
string, in other words you use quotes when defining the number.
- The answer is not actually a number, but a word or sentence.
In the first case since the answer is stored in a
string and the response is stored as an
int they will fail the type equality test that the
=== operator enforces, and therefore appear unequal even if the number are in fact the same. In the second case
parseInt will convert the response into a value known as
NaN (Not a Number) since the response it is asked to convert is in fact not a number.
Looking at your code I see that your questions fit point 1. I recommend changing the answers into pure numbers, in other words store them as
50 and not
'50' and the like. If you decide to add questions that have words as answer then not using
parseInt is in fact the right thing to do.