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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Brian Prouty
Brian Prouty
1,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...

https://imgur.com/a/FzYnyfr

1 Answer

andren
andren
28,558 Points

The 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:

  1. The answer is not stored as an int but as a string, in other words you use quotes when defining the number.
  2. 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.

Brian Prouty
Brian Prouty
1,792 Points

Thank you for explaining that for me :)