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 trialGary Burner
10,167 PointsCorrect 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
231,269 PointsThe 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.