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 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
215,960 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.