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

Tiffany White
Tiffany White
5,373 Points

This is the only challenge I did and this is pretty close

Here is my code

var quiz =  [
  ['What is the name of the website with the old collection of BBS hacker files?', 'Textfiles'],
  ['What is the name of the first notorious hacker group from the 80s?', 'Legion of Doom'],
  ['What is the most hackable OS for mobile?', 'Android'],
];
var answer1;
var answer2;
var answer3;
var correct = 0;

  for ( var i = 0; quiz.length < 1; i += 1 ) {
      while ( false ) {
        answer1 = prompt(quiz[i][0]);
        answer2 = prompt(quiz[1][0]);
        answer3 = prompt(quiz[2][0]);
        answer1 = answer1.toLowerCase();
        answer2 = answer2.toLowerCase();
        answer3 = answer3.toLowerCase();
    if ( answer1 === 'Textfiles' ) {
      correct += 1;
    }
    if ( answer2 === 'Legion of Doom') {
      correct += 1;
    }
    if ( answer3 === 'Android') {
      correct += 1;
    }
}



function print(message) {
  document.write(message);
}
Tiffany White
Tiffany White
5,373 Points

It isn't clean by any means but this the closest I have ever gotten. I didn't finish before I looked at this solution. Can you tell me why I shouldn't do

prompt(quiz[1][0]);

?

Tiffany White
Tiffany White
5,373 Points

My code is hard to read up there. Here it is. Needed a tip on the Markdown syntax.

var quiz = [
  ['What is the name of the website with the old collection of BBS hacker files?', 'Textfiles'],
  ['What is the name of the first notorious hacker group from the 80s?', 'Legion of Doom'],
  ['What is the most hackable OS for mobile?', 'Android'],
];
var answer1;
var answer2;
var answer3;
var correct = 0;

  for ( var i = 0; quiz.length < 1; i += 1 ) {
      while ( false ) {
        answer1 = prompt(quiz[i][0]);
        answer2 = prompt(quiz[1][0]);
        answer3 = prompt(quiz[2][0]);
        answer1 = answer1.toLowerCase();
        answer2 = answer2.toLowerCase();
        answer3 = answer3.toLowerCase();
    if ( answer1 === 'Textfiles' ) {
      correct += 1;
    }
    if ( answer2 === 'Legion of Doom') {
      correct += 1;
    }
    if ( answer3 === 'Android') {
      correct += 1;
    }
}



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

1 Answer

Christian Weatherford
Christian Weatherford
8,848 Points

Couple things. You're missing a closing curly brace, looks like either for your for-loop or your while-loop.

Your while loop will never execute since you're using the literal "false" as a condition. Your for-loop will also never execute because you're using "quiz.length < 1" where you probably want to use "i < quiz.length". I'd say the while-loop isn't necessary, or even the for-loop with how you're asking the quiz questions and comparing them to the correct answers. But, if you want to iterate through your array containing the questions in that fashion, you'll probably want to store the correct answers in an array as well, so that you can iterate through the answers too. Your iteration logic would be some thing like:

for (/*every question in the quiz array*/) {
  //prompt the user with the question located at the current index/for-loop variable
  //compare their answer to the corresponding answer located at the current index/for-loop variable
  //if they match, increase the correct counter
}

Finally, in your code you're converting the user's answers to lower case but comparing them to strings that have upper case characters. So the user will always be wrong.