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

Mark Brady
Mark Brady
6,239 Points

My counter won't go passed 1.. Any thoughts?

Here is my code. Thanks in advance!

function print(message) {
  document.write(message);
}

var question;
var answer;
var response;
var rightAnswers = 0;
var html;


var questions = [
  ['How many letters are in my first name?', 4],
  ['What is my age?', 23],
  ['How old is my girlfriend', 24]
];

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = parseInt(prompt(question));
} if ( response === answer ) {
  rightAnswers += 1;
} 

html = "You got " + rightAnswers + " question(s) right";
print(html);

2 Answers

andren
andren
28,558 Points

There is a mistake in your code, but to be fair to you it took me quite a bit to notice it. I even opened an IDE and played around and debugged the code because I was curious as I could not notice any obvious issues.

The problem stems from the fact that your if statement is placed after your for loop code block, in other words you run though the entire for loop (prompting for answers) before you check that the response and answer match, this means that only the last question will actually have it's answer checked. Which is why the maximum score you can get is 1.

Moving the if statement inside the for loop like this:

function print(message) {
  document.write(message);
}

var question;
var answer;
var response;
var rightAnswers = 0;
var html;


var questions = [
  ['How many letters are in my first name?', 4],
  ['What is my age?', 23],
  ['How old is my girlfriend', 24]
];

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = parseInt(prompt(question));
  if ( response === answer ) {
    rightAnswers += 1;
  } 
}

html = "You got " + rightAnswers + " question(s) right";
print(html);

Will fix the issue.

Mark Brady
Mark Brady
6,239 Points

Thank you for your help! I couldn't see it and it looked for quite a while.

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Mark,

Try putting your if statement that is incrementing the rightAnswer variable inside of the for loop, instead of outside. With it on the outside, the for loop runs all iterations before getting to the if statement, which now only get executed once, and that is why it only reaches a count of one. :)

Keep coding! :dizzy: