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 2 Solution

Dinesh Kumaar Rajendran
Dinesh Kumaar Rajendran
3,395 Points

Need help , below doesn't work

 var questions = [
    ['How many states are there in United states of America',50],
    ['Any odd number',1],
    ['Any even number',2]
    ];
var correct=[];
var wrong=[];


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

      function buildList(arr){
        var listHtml ='<ol>';
        for(var i=0; i<arr.length; i=+1){
          listHtml += "<li> "+ arr[i] + " </li>";

        }
        listHtml += '</ol>';
        return listHtml;
      }

    var question;
    var answer;
    var response;
    var html;
    var correctAnswer =0;
    for(var i =0; i<questions.length;i++){
      question = questions[i][0];
      answer = questions[i][1];
      response = parseInt(prompt(question));

      if (response === answer){
        correctAnswer +=1;
        correct.push(question);
              }
           else{
             wrong.push(question);
           }
    }

    html ="You got " + correctAnswer + " questions right.";
    html += '<h2> You got these correct answer </h2>';
    html += buildList(correct);
    html += '<h2> You got these wrong answer"</h2>';
    html += buildList(wrong);
    print(html);

1 Answer

Hi Dinesh

can you be more specific of the error please?

However scanning through your code i can see that you incorrectly looping through the function buildList()

in your for loop you iterate with =+

to be syntacically correct, this should be += like the below, or i++ for short:

      function buildList(arr){
        var listHtml ='<ol>';
        for(var i=0; i<arr.length; i+=1){
          listHtml += "<li> "+ arr[i] + " </li>";

        }
        listHtml += '</ol>';
        return listHtml;
      }

Everything else looks correct from a first glance, if this doesn't fix it can you paste in your console error

Also, in your questions array, your question is any odd or even number but your answer is only for 1 and 2, you can improve this to cater for every odd an even number with the modulo operator, see if you can work out how!

Good Luck