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 trialVictor Gordian
4,656 PointsIts not showing the right answers
var questions = [
['What color is the sky', 'blue'],
['whats the begining of the Aplhabet', 'a'],
['when its hot people usually like to eat what?', 'ice cream']
];
var correctA = 0;
var question;
var answer;
var response;
var html;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
for (var i = 0; i < questions.length; i += 1){
question = questions[i][0];
answer = questions[i][1];
response = parseInt(prompt(question));
if (response === answer) {
correctA += 1;
}
}
html = "You got " + correctA + " question(s) right."
print(html);
1 Answer
Matthew Saul
11,354 PointsThe reason it's not working is related to the parseInt() function. Each time you're running that in your code it is coming up with this:
parseInt("blue");
NaN
NaN = Not a Number
So, if you simply just write your code where you omit parseInt or write another if statement to include parseInt so an answer can still be a number, you'll be fine.
for (var i = 0; i < questions.length; i += 1){
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
// instead of // response = parseInt(prompt(question));
if (response === answer) {
correctA += 1;
}
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsHi Matthew Saul, I've updated your answer to include code blocks to make it easier to read, and hopefully be accepted as the best answer!