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

My solution. Is it okay. or need to refactoring?

var question = [
  ['What is our national animal?', 'tiger'],
  ['What is our national flower?', 'rose'],
  ['What is our natinal fish?','hilsha']
];
var questionBox;

var correctAnswer = "<h2> Number of Correct answer</h2><ol>";
var wrongAnswer = "<h2> Number of Wrong answer</h2><ol>";
function print(message) {
  document.write(message);
}

for ( var i = 0; i < question.length; i++ ) {
 var questionBox = prompt(question[i][0]);
  questionBox.toLowerCase();
  if ( questionBox === question[i][1] ) {
   correctAnswer += '<li>' + question[i][0] + '</li>';
  } else {
    wrongAnswer += '<li>' + question[i][0] + '</li>';
  }
}

correctAnswer += '</ol>';
wrongAnswer += '</ol>';
var html = correctAnswer + wrongAnswer;
print(html);

1 Answer

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Here are a few suggestions

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script>

    var question = [
      ['What is our national animal?', 'tiger'],
      ['What is our national flower?', 'rose'],
      ['What is our natinal fish?','hilsha']
    ];

    var questionBox;

    var correctAnswer = "Number of Correct answers: ";
    var wrongAnswer = "Number of Wrong answers: ";
    var html = '';

    //new Variables (see below)
    var totalCorrect = 0;
    var totalWrong = 0;

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

    for ( var i = 0; i < question.length; i++ ) {

      //don't use var again inside your loop. You used it at the top
      //var questionBox = prompt(question[i][0]);
      questionBox = prompt(question[i][0]);

      //the variable name "questionBox" works but I would use something 
      //that makes it more clear what it is like "userResponse"
      questionBox.toLowerCase();


      //your if/else logic is correct but I'm not sure what you are trying
      //to do inside the test. I will assume that you want to tell the user
      //the number of questions they got correct?
      if ( questionBox === question[i][1] ) {
        //correctAnswer += '' + question[i][0] + '';
        totalCorrect++;
      } else {
        //wrongAnswer += '<li>' + question[i][0] + '</li>';
        totalWrong++;
      }

    }

    correctAnswer += totalCorrect;
    wrongAnswer +=  totalWrong;

    //use var to make your variables at the top of the script
    //var html = correctAnswer + wrongAnswer;
    html = correctAnswer + '<br>' + wrongAnswer;

    print(html);

  </script>
</body>
</html>

thnx mate :)