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

Elizabeth Chai
Elizabeth Chai
9,692 Points

undefined popping up after loop printed

The code runs but the only thing is after the results pop up it looks like: You got 3 out of 4 questions right.

Questions you got right: undefined What color is produced by mixing blue and yellow?

Question you got wrong: undefined What color is produced by mixing green and blue?

Don't know what's going on???

My code is as follows:

var quizList= [
  ["What color is produced by mixing blue and yellow?", "green"],
  ["What color is produced by mixing blue and red?", "purple"],
  ["What color is produced by mixing yellow and red?", "orange"],
  ["What color is produced by mixing green and blue?", "aqua"]
];

var correctAnswer=0;
var wrongAnswer=0;

var questionCorrect;
var questionWrong;

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

for (i = 0; i < quizList.length; i += 1){
  var answer = prompt(quizList[i][0]);
  answer = answer.toLowerCase();
   if (answer === quizList[i][1]){
    correctAnswer += 1;  
    questionCorrect += "<p>" + quizList[i][0] + "</p>";
    } else {
    wrongAnswer += 1;  
    questionWrong += "<p>" + quizList[i][0] + "</p>";
    } 
  }

document.write("<h2> You got " + correctAnswer + " question\(s\) out of "  + quizList.length + " right. </h2>");

document.write("<h2> Questions you got right: </h2> " + questionCorrect);

document.write("<h2> Questions you got wrong: </h2>" + questionWrong);

2 Answers

Leandro Botella Penalva
Leandro Botella Penalva
17,618 Points

Hi Elizabeth,

You need to initialize the variables questionCorrect and questionWrong with an empty string "" before concatenating any value otherwise you are concatenating your question to the default "undefined" variable value.

Elizabeth Chai
Elizabeth Chai
9,692 Points

Thanks! i did not realize this quiz had multiple parts .