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

Kelly Walsh
Kelly Walsh
3,642 Points

Sharing Quiz Attempt

I started the javascript objects section and realized that I didn't have as good of a grasp on the javascript foundations as I probably needed. So, I've been going through the lessons again, taking more care/time (and taking notes) with the lessons and challenges (also, not skipping ahead to see the solution before I have come up with my own working solution).

With that, I wanted to share my quiz, part 1, that I made because I actually got it this time around (yes, I'm excited)! Feel free to provide feedback or suggestions.

//ask questions -- use a two-dimensional array to store questions and answers
//display # of correct answers at end of quiz ---keep track of # of correct answers 
//use a conditional statement to see if user answer === quiz answer [i][1]
//iterate through the questions using a for loop questions = [i][0]

var quiz = [
['Who is the front woman of the band, Hole?' , 'courtney love'], 
['Who was the front man of Nirvana?' , 'kurt cobain'], 
['Which band did Nirvana\'s drummer form in 1994?', 'foo fighters']
];

var correct = 0; 

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

for (var i = 0; i < quiz.length; i ++ ){
    var answer = prompt(quiz[i][0]);

if ( answer.toLowerCase() === quiz[i][1] ){
        correct++; 
    } 
}
print('You got ' + correct + ' out of 3 questions right.'); 

1 Answer

Hi Kelly Walsh, your code works, and is nice and succinct. The only suggestion I'd make is to use a variable in your print statement at the bottom so that if you add more questions, you don't have to change the number there manually.