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

Check my code out and comment please. This was how I worked it out, totally different and maybe more difficult approach

var questions = [ ['How many states are in the United States?', 50],
                  ['How many continents are there?', 7],
                  ['How many legs does an insect have?', 6]
                ];
var right = 0;
var correct = '<ol>';
var wrong = '<ol>';

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

for( i = 0; i < questions.length; i += 1 ) {
  answers = prompt(questions[i][0]);
  if ( parseInt(answers) === questions[i][1] ) {
    right += 1;
    correct += '<li>' + questions[i][0] + '</li>';  
  } if ( parseInt(answers) !== questions[i][1] )
  {
    wrong += '<li>' + questions[i][0] + '</li>';
  }
}

if ( right === 0 || right === 2 ) {
  print('You got ' + right + ' question(s) right.');
} else {
  print('You got ' + right + ' question right.');
}


print('<h2>' + 'You got these questions correct:' + '</h2>');
print(correct);

print('<h2>' + 'You got these questions wrong:' + '</h2>');
print(wrong);
Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,696 Points

Updated the formatting for you. Refer to the Markdown Cheatsheet for examples.

Thank you!

2 Answers

Pretty intuitive approach I can say. Good job.

Well thank you...I could have done better with the naming as he did with the 'question', 'answer', 'response' approach

Michal Weizman
Michal Weizman
14,050 Points

Did you really need the second "if" for checking the un-correctness of the answers? Or was it enough to put an "else" instead?

if ( parseInt(answers) === questions[i][1] ) {
right += 1;
    correct += '<li>' + questions[i][0] + '</li>';  
  } else {
    wrong += '<li>' + questions[i][0] + '</li>';
  }
}

Nope, I see that it was not necessary, thanks Michal