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

Ivana Lescesen
Ivana Lescesen
19,442 Points

Can someone help me ?

What is wrong with my code? Thank you so much :)

var correctAnswer = 0;

var questions = [
  ['what is the capital of sweden', 'stokholm'],
  ['What is my favorite TV show', 'COMMUNITY'],
  ['What is my favorite movie', 'Love Actually'],
];

var answer;


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


function askQuestions( quiz ) {
  for ( var i = 0; i < quiz.length; i += 1) {

     answer = prompt(quiz[i][0]);
     answer = answer.toLowerCase();

        if (answer == quiz[i][1]){
           correctAnswer+=1;
          print( 'That is correct ');
         } else {
            print( 'sorry '); 
            }
   }

}


askQuestions(questions);


document.write("<p>You got " + correctAnswer + " out of 3 questions correct.</p>")

Moderator Edited: Added Markdown to the code block so it is readable in the forum. Please refer to the Markdown cheatsheet for formatting instructions. <code> elements don't work with Markdown.

Steven Parker
Steven Parker
215,939 Points

:x: message is a function argument. Those don't need to be defined.

1 Answer

Steven Parker
Steven Parker
215,939 Points

:point_right: You didn't describe your problem, but I do notice one functional issue:

It looks like you're converting the user's answer to lower case, but then comparing it to stored answers which contain upper case characters. Only answers stored as lower case can match a correct answer.

Happy coding!   -sp:sparkles: