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

Devon Deason
Devon Deason
7,061 Points

For part 1: My code works, but looks way different than Dave's solution .

The code I came up is quite different from the solution Dave gives. As far as I can tell it operates the same way and gives the same results for PART 1. Is his method better, or did I just find a more succinct way to do the same thing?

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


var quiz = [
  ['Who is the Green Arrow?', 'Oliver Queen'],
  ['Who is Deadpool?', 'Wayde Wilson'],
  ['Who is the Flash?', 'Barry Allen'],
  ['Who is Peter Parker?', 'Spiderman'],
  ['Who is Batman?' , 'Bruce Wayne']
];
var correct = 0;
  for (var i = 0; i <=4; i += 1){
  var answer = prompt(quiz[i][0]);
    if (answer === quiz[i][1]) {
        correct += 1;
  }
  }

var html= 'You got ' + correct + ' questions correct, and ' + (5 - correct) + ' questions wrong.';

 print(html);

//Fixed Code Presentation

1 Answer

I am not sure what his code looks like but yours look pretty clean. I wouldn't change much other than create a variable "wrong" and extending the if statement with an else statement that adds 1 to the wrong counter. Like this.

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

var quiz = [
    ['Who is the Green Arrow?', 'Oliver Queen'],
    ['Who is Deadpool?', 'Wayde Wilson'],
    ['Who is the Flash?', 'Barry Allen'],
    ['Who is Peter Parker?', 'Spiderman'],
    ['Who is Batman?' , 'Bruce Wayne']
];

var correct = 0,
    wrong = 0;

for (var i = 0; i <= 4; i += 1){
    var answer = prompt(quiz[i][0]);
    if (answer === quiz[i][1]) {
        correct += 1;
    } else {
        wrong += 1;
    }
}

var html= 'You got ' + correct + ' questions correct, and ' + wrong + ' questions wrong.';

print(html);

very minimal change but makes it easier to read and less code to change if you added more questions.

I hope this helps.

Devon Deason
Devon Deason
7,061 Points

Awesome, thanks for the input!