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
Cezary Burzykowski
18,291 PointsRefference Error: Variable not defined
Hello there!
I have almost all the code done but unfortunatelly I get the refference error when I try to print out questions user answered correctly.
Here is the snippet of my code:
var quiz = [
["What is the main religion of Israel?", "Judaism"],
["What word stands for 'truth' in Hebrew?", "Emet"],
["Is Donald Trump an idiot?", "Yes"]
] ;
var correctAnswers = 0;
var correctQuestions = [];
var wrongQuestions = [];
function print(message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
for ( i = 0; i < 3; i += 1) {
var userAnswer = prompt(quiz[i][0]);
if ( userAnswer.toUpperCase() === quiz[i][1].toUpperCase()) {
correctAnswers += 1;
correctQuestions.push(quiz[i][0]);
alert("That's correct!");
}
else {
alert("Wrong, correct answer was '" + quiz[i][1] + "'")
wrongQuestions.push(quiz[i][0]);
}
}
print("You answered " + correctAnswers + " correctly");
print("Here are the questions you answered correctly:" + "<ol><li>" + correctQuestions[0] + "</li>" + "<li>" + correctQuestions[1] + "</li>" + "<li>" + correctQustions[2] + "</li></ul>");
print("Here are the questions you didn't answer correctly:" + "<ol><li>" + wrongQuestions[0] + "</li>" + "<li>" + wrongQuestions[1] + "</li>" + "<li>" + wrongQustions[2] + "</li></ul>");
The refference error is on the line 32 and it points out that var correctQuestions is not defined. It's a little confusing for me because when I try to call out this variable for example by alert(correctQuestions) everything works fine.
I would appreciate any help :)
2 Answers
Steven Parker
243,656 Points
It looks like you just have a couple of typo's:
The variable "correctQuestions" is referenced 3 times on that line, but the third time it is spelled "correctQustions" (missing an "e").
Similarly, on the following line you will find "wrongQustions" with the same error.
Cezary Burzykowski
18,291 PointsThank you Steven, it's funny how easy it is to miss rockie mistake like this