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

Martin Krkoska
Martin Krkoska
10,514 Points

Hello, could you please review my code ?

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

var questions = [
  ['How many states are in the United States ?',50],
  ['How many legs does a spider have ?',8],
  ['What color the sky have ?','blue'] 
]

/*
return array with two arrays 1. with correct answers and  2. with wrong 
*/ 
function quiz(questions) {
  var correct = [];
  var wrong = [];
  for ( var i = 0; i < questions.length ; i += 1) {
    if (questions[i][1] == prompt(questions[i][0])) {
      correct.push(questions[i])
    } else {
      wrong.push(questions[i])
    }
  }
  var answers = [];
  answers.push(correct,wrong);
  return answers;
}

/* 
Putting together html 
1. number correct answers (answers[0].length) 
2. questions correct 
3. questions wrong
*/
function inToHtml(answers) { 
  var html = '';
  html += '<h3> You got ' + answers[0].length + ' question(s) right. </h3>'
        + '<h2> You got these questions correct: </h2>'
        + '<ol>';
  for ( var i = 0; i < answers[0].length ; i += 1 ) {
    html += '<li>' + answers[0][i][0] + '</li>';
  }
  html += '</ol>' 
        + '<h2> You got these questions wrong: </h2>'
        + '<ol>';
  for ( var i = 0; i < answers[1].length ; i += 1 ) {
    html += '<li>' + answers[1][i][0] + '</li>';
  }
  html += '</ol>';
  return html; 
}


print (inToHtml (quiz (questions)));

2 Answers

Özgür Ozan Çakmak
Özgür Ozan Çakmak
11,451 Points

It looks good and if it is working, kudos to you! Only problem I had while reading is this part

 if (questions[i][1] == prompt(questions[i][0])) {

I think it would read better if you had done something like this

var questionData = questions[i];
var questionText = questionData[0];
var questionAnswer = questionData[1]

if(questionAnswer === prompt(questionText){
Martin Krkoska
Martin Krkoska
10,514 Points

Yes you are right , thank you.