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 Solution

Bei Mi Chen
seal-mask
.a{fill-rule:evenodd;}techdegree
Bei Mi Chen
Full Stack JavaScript Techdegree Student 6,720 Points

Any way I can optimize my code? Or any way I can make it clearer?

Here's my code:

var questionAnswer = [
  ['Which planet is the closest to the Sun?', 'mercury'],
  ['What\'s the best coding language?', 'javascript'],
  ['What\'s the name of fourth farthest planet from the Sun', 'mars']  
];
var answerCorrect = 0;
var answerWrong = questionAnswer.length; 

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

for( var i = 0; i < questionAnswer.length; i += 1) {
userAnswer = prompt(questionAnswer[i][0]); 
    if ( userAnswer.toLowerCase() === questionAnswer[i][1]) {
         answerCorrect += 1;
         answerWrong -= 1; 
   }
}

var results = "<p> You answered " + answerCorrect + " correctly and " + answerWrong + " incorrectly.</p>"; 

print(results);

Criticisms are welcome.

Thanks!

1 Answer

Steven Parker
Steven Parker
231,269 Points

Looks like you nailed it to me. :+1: Good job!