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 2

Pratik Rai
Pratik Rai
6,856 Points

having problem when an answer is set to a string value.

this is my approach http://codepen.io/anon/pen/qdBwbG?editors=101 which works perfectly fine but the problem is when an answer is set to a string value suppose 'phillips', because of the parseInt I am getting negative results. And without the parseInt method the numbers are not registered. How do we overcome the problem, when the answers are suppose to be the mix of number value as well as string or boolean?

3 Answers

When dealing with user input, there can be all kinds of corner cases to consider. These suggestions will get you closer to handling questions with mixed numeric and string answers.

  1. Store all correct answers as strings in either all caps or all lowercase (or numbers).

  2. I don't thing you need to parseInt, because if the user types in mixed numbers and letters then the answer is wrong.

  3. Use either toUpperCase() or toLowerCase() on the user input as these functions will leave your numeric values untouched in the string.

This won't handle the case where the user types: '6 bullets'. If you want to generically handle cases like that, you'd have to start checking the type on the correct answer and parse accordingly. I think that's beyond the scope at this point.

I tested this and it works pretty well. You could add a question asking 'What color is the sky?' and it will work properly.

    var questionAnswer = [
        ['how many tooth does an elephant have?', '2'],
        ['how many bullets is in a pistol?', '6'],
        ['count the number of sense organ we have', '5'],
    ];
.
.
.
        playerAnswer = prompt(question).toUpperCase();
Pratik Rai
Pratik Rai
6,856 Points

nope its not working. without parseInt i got all the answers wrong.

I love that you put your code in codepen because I can actually make the changes and test it! I can think of only 2 reasons that your answers would still be wrong. If you use toUpperCase() like I did above, then the answers you store at the beginning must be in uppercase as well. I did test the changes in codepen and it works there for both number and letter answers, but maybe there are some browser incompatibilities? That seems unlikely, and I'm guessing that you are still trying numeric answers, and that's not working. So, I'm stumped. You could try == instead of === so that the variable type is not a factor.

You can also fork a CodePen to create your own, as I have down, and followed your suggestions.

Seems to work for me with both numbers and strings as answers: http://codepen.io/iainsimmons/pen/ojNJLW

Pratik Rai
Pratik Rai
6,856 Points

hammy thanks jacie.