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

My code works but doesn't look exactly the same..

  var correctAnswer = [];
  var wrongAnswer = [];
  var numberCorrect =  0;
  var numberWrong = 0 ;

  var questionAndAnswer = [
    ["2 + 2 = ? ", 4 ],
    ["3 - 1 = ? ", 2 ],
    ["6 + 2 = ? ", 8 ],
    ["9 - 3 = ? ", 6 ],
    ["4 + 5 = ? ", 9 ],
  ]

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

function questionList(list) {
  var html = "<ol>";
  for ( i = 0; i < list.length; i += 1) {
    html += "<li>" + list[i] + "</li>";
  }
    html += "</ol>"; 
    print(html);
}

  for ( i = 0; i < questionAndAnswer.length; i += 1 ) {
    var answer;
    answer = prompt( "Can You solve it ?  " + questionAndAnswer[i][0] );
    if ( parseInt(answer) ===  questionAndAnswer[i][1] ) {
      numberCorrect += 1;
      correctAnswer.push(questionAndAnswer[i][0]);
    } else {
      numberWrong += 1; 
      wrongAnswer.push(questionAndAnswer[i][0]);
    }

  }
  print("<h2>You got " +  numberCorrect + " question(s) right.</h2>");
  print("You got these question(s) correct: ");
  questionList(correctAnswer)  ;
  print("<h2>You got " +  numberWrong + " question(s) wrong.</h2>");
  print("You got these questions wrong: ") ;
  questionList(wrongAnswer) ;

1 Answer

Steven Parker
Steven Parker
231,236 Points

There's almost never just one way to write a program. And as the tasks become more difficult, you can expect that your solutions will differ from the examples. But you can enhance your learning by examining the differences and making sure that you understand the example method as well as your own.

It's also quite possible that you discovered a more efficient and/or concise way of coding the same functionality. That's just evidence of your learning progress.