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 trialPratik Rai
6,856 Pointshaving 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
Jacie Fortune
8,969 PointsWhen 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.
Store all correct answers as strings in either all caps or all lowercase (or numbers).
I don't thing you need to parseInt, because if the user types in mixed numbers and letters then the answer is wrong.
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();
Jacie Fortune
8,969 PointsI 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.
Iain Simmons
Treehouse Moderator 32,305 PointsYou 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
6,856 Pointshammy thanks jacie.
Pratik Rai
6,856 PointsPratik Rai
6,856 Pointsnope its not working. without parseInt i got all the answers wrong.