Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Gary Burner
Gary Burner
10,167 Points

Correct and incorrect answers are not summing properly

Im having trouble getting the right and wrong answers to sum correctly in the string literal. I've instantiated the variables in many different spots in the code but none of them seem to work. The console.log(right / wrong) checks below the loop are generating the correct value. Any help would be appreciated!

Thanks, Gary

let right = 0
let wrong = 0

let questionPairs = [
  ["What color is Butters?", "brown"],
  ["What language is this?", "javascript"],
  ["What game is Colleen playing?", "jeopardy"]
];

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

listHTMLright = `<h1>You got ${right} questions correct, they were: </h1>` //cant get ${} to generate the correct number
listHTMLwrong = `<h1>You got ${wrong} questions incorrect, they were: </h1>` //cant get ${} to generate the correct number

for ( i = 0; i < questionPairs.length; i += 1 ) {
  questionAnswer = prompt(questionPairs[i][0]).toLowerCase()
  if ( questionAnswer === questionPairs[i][1]) {
    listHTMLright += `<li>${questionPairs[i][0]}</li>`;
    right += 1
  } else {
    listHTMLwrong += `<li>${questionPairs[i][0]}</li>`;
    wrong += 1    
  }
}

console.log(right) //check that right is the correct value
console.log(wrong) //check that wrong is the correct value

combined = listHTMLright + listHTMLwrong;

print(combined)

1 Answer

Steven Parker
Steven Parker
220,425 Points

The message strings "listHTMLright" and "listHTMLwrong" are both being assigned before the questions are asked, when the scores are zero.

To show correct scores, the total messages would need to be added after the question loop, near where the log messages are done.