Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Lefora Williams
Lefora Williams
2,486 Points

Problems with .toLowerCase and print()

Hi guys,

my code is running fine apart from two things...

  1. I've added the .toLowerCase() command to the conditional statement but in the quiz it does not accept alternative spelling of the word.

  2. My message will not print to the document.

Can you please look over my code and help me find the mistakes?

Thanks a lot!

Also, I can't find a way to post my code with markup sorry (Now I do! Thanks Adam Beer :D )

// Two dimensional array with 3 questions
var quiz = [
  ["What is the Capitol of Germany?", "berlin"],
  ["What's World in german?", "welt"],
  ["Where is London", "uk"]
];
var correct = 0;
var score = "";

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

//Loop to cycle through the quiz qestions and answers
alert("Welcome to the quiz! Are you ready? Alright let's get started!");

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

  if (prompt(quiz[i][0])=== quiz[i][1].toLowerCase()) {
  alert ("Yes! That's right!");
    correct += 1;
  } 
    else {
      prompt ("Nope, sorry! The right answer is " + quiz[i][1]);

  } score = " <p>You've got " + correct + " out of 3 questions right </p>" ;
} 


print(score);
Adam Beer
Adam Beer
11,308 Points

Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```

2 Answers

Chris Pulver
Chris Pulver
7,624 Points

You're on the right track!

Take a look at a couple things:

  1. for (i=0; i <= quiz[i].length; i++) here you have not declared "i". Also, you are asking for the loop to look at the length of the "i" index of quiz when you are wanting to look at the length of quiz as a whole. Lastly, check your conditional. If you are starting at 0, and there are three questions in your array, this loop will run four times.
  2. Your toLowerCase() method is currently attached to the answers in your quiz array, but you want to make the supplied answer from the student be lower case.
  3. Check the placement of where you are updating the value of "score".
  4. Your else statement is currently prompting the student when the answer is wrong. This provides a text field as opposed to just a message.
Lefora Williams
Lefora Williams
2,486 Points

Hi Chris,

Thanks so much for your help! Your feedback has helped me a lot and I finally got my quiz to run the way I want it to !

Chris Pulver
Chris Pulver
7,624 Points

You're welcome! Glad it was helpful

You do not need to access the index of the quiz array when you're initializing your loop, and you need to account for the zero-based index of the array. So the array's indexes range from 0-2 but the length in this case is equal to three. Initialize the loop like this:

for (i=0; i <= quiz.length -1; i++)
Adam Beer
Adam Beer
11,308 Points

I did not come to the -1. Bravo!