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

My solution. Tips on refactoring?

I was going for readability, but maybe I should have build a more modular functions rather than one massive for loop.

var questionBank = [
  ['William Lyon Mackenzie King was a longtime leader of which nation?', 'Canada'],
  ['Which team, led by Johnny Bench, won the 1975 World Series?', 'Cincinnati Reds'],
  ['In which country was Ferdinand Magellan killed?', 'Philippines']
];

var correct = 0;
var inCorrect = 0;

for (i = 0; i < questionBank.length; i++) {
  document.write('<h2> <strong>Question:</strong> ' + questionBank[i][0] + '</h2>');
  var answer = prompt(questionBank[i][0]);

  if (answer.toUpperCase() === questionBank[i][1].toUpperCase()) {
    correct += 1;
    document.write('<p><strong>' + answer + '</strong> is the correct answer!</p>');
  } else {
    inCorrect += 1;
    document.write('<p><strong>' + answer + '</strong> is the incorrect answer!</p>')
  }
}

document.write('You got ' + correct + ' guesses right, and ' + inCorrect + ' guesses wrong.');

``

1 Answer

Hi I think your solution is great. For readability, maybe some comments. But the solution is already readable enough.