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

Aaron Fryer
Aaron Fryer
3,999 Points

Why isn't my quiz working with strings?

Hey Guys,

1st time I've asked a question on here so sorry if it formats bad....

I created my quiz as challenged in the previous video,. Admittedly, I haven't made it easy on myself by asking for strings as answers, but I still can't understand what I've done wrong.

When testing the below code, the answers I give are correct if I type them out all in lower case, as you would expect. However, when capitalising the first letter etc, my "toLowerCase" doesn't seem to work, and it says I got the answer wrong. E.G Question 1 - Answer : bones - CORRECT Answer: Bones - INCORRECT

I appreciate I haven't formatted the prints too well, but I'm more concerned with the JScript for now

Any advice would be awesome, apologies for the context of the questions, my 3 childhood toys were all I could think of!

Cheers, Aaron

    var correctGuesses = [];
    var incorrectGuesses = [];
    var numberCorrect = 0;
    var questions = [
      ["What is Doggy's favourite food", "bones"],
      ["What is Clowny's favourite method of transport?", "unicycle"],
      ["What is Bear's favourite activity?", "sleeping"]  
    ];

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


      for (var q=0; q < questions.length; q++){
        question = questions[q][0];
        answer = questions[q][1];
        response = prompt(question.toLowerCase);
        if (response === answer){
          numberCorrect += 1;
          correctGuesses.push(questions[q][0]);
        } else {
          incorrectGuesses.push(questions[q][0]);
        }
      }

    print('You scored: ' + numberCorrect + '<br>');
    print('The questions you got right were: ' + correctGuesses.join(', ') + '<br>');                           
    print('The questions you got wrong were: ' + incorrectGuesses.join(', ') + '<br>');

//Fixed Code Presentation

1 Answer

Your toLowerCase needs to be in your conditional statement rather than on the question.

for (var q=0; q < questions.length; q++){
    question = questions[q][0];
    answer = questions[q][1];
    response = prompt(question);
    if (response.toLowerCase() === answer){
      numberCorrect += 1;
      correctGuesses.push(questions[q][0]);
    } else {
      incorrectGuesses.push(questions[q][0]);
    }
  }

I hope this helps

Aaron Fryer
Aaron Fryer
3,999 Points

That's awesome, can't believe I didn't spot it, thanks Chyno!!