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

Build a Quiz: Review my code please?

Hey everyone! This is my code below into building a quiz generator and having a loop go through each question until it's finished. I think it does have some refactoring that does need to be done because when the final page loads after all questions are asked, I do get an undefined statement coming right before either the correct or incorrect questions statements. I messed around with the code for quite a bit before I got what I wanted...

var questions = [
  ['1. What is the name of a snake programming language?','python'],
  ['2. What is the name of the program that sounds like a gem?','ruby'],
  ['3. What is the name of the program that sounds like coffee?','javascript']
];

var correctAnswers = 0;
var correctQuestions;
var incorrectQuestions;


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

for ( var i = 0; i < questions.length; i += 1 ) {
  var guess = prompt(questions[i][0]);
  if ( guess.toLowerCase() === questions[i][1] ) {
   correctQuestions += '<p>' + questions[i][0] + '</p>';
   correctAnswers += 1;
  } else if ( guess.toLowerCase() !== questions[i][1] ) {
   incorrectQuestions += '<p>' + questions[i][0] + '</p>';
  }   
} 

print('<p>You got ' + correctAnswers + ' question(s) right.</p>' );
print('<p>You got these questions correct: </p>' + '<p>' + correctQuestions + '</p>');
print('<p>You got these questions wrong: </p>' + '<p>' + incorrectQuestions + '</p>');

2 Answers

Hello!

There are 2 variables you're not initializing, but using: correctAnswers and incorrectAnswers. You can't add (+=) a value to their current value because they have none. Try initializing them with an empty string ('').

Thank you so much. I highly appreciate you responding quickly there. But yes, I went ahead and put a value of an empty string for the variable and it worked out perfectly fine :).

I came up with this on my own, can you take a look ? :

function print(message) {
  document.write(message);}
var questions = [
  ['Capital of the United States ?', 'washington'],
  ['Capital of Brazil?', 'brasilia'],
  ['Capital of Ireland ?','dublin']
];var correctAwnsers = [];var wrongAwnsers =[];
//asking questions
for (i=0; i<3; i+=1){
anwser = prompt (questions[i][0]);
  if ( anwser.toLocaleLowerCase() === questions[i][1] )
  {correctAwnsers += '<li>'+ questions[i], + '</li>';} else {wrongAwnsers+= '<li>'+ questions[i], + '</li>';}
}
//printing out values
if (wrongAwnsers) {
print ('<h1>You got this ones wrong : </h1>')  
print (wrongAwnsers);
}
if (correctAwnsers){
print ('<h1>You got this ones right : </h1>')
print (correctAwnsers);
}```