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

Rick de Jong
Rick de Jong
3,305 Points

Is this the most efficient way? (33 lines of code in 2:50)

So I just did the challenge in 2 minutes and 50 seconds. What do you guys think of the efficiency of this program? It's only 33 lines of code!

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

var questions = [
    ["What's the best Country in the world?", "holland"],
    ["Who was the previous CEO of Apple?", "steve jobs"],
    ["Who is the president of Russia?", "putin"]
];

var answersList = [
  ['Correct Answers: '],
  ['Wrong Answers: ']
];

var answersGiven;

while (true){
  for (var i = 0; i< questions.length; i+=1){
    var answer = prompt(questions[i][0]);
    if (answer.toLowerCase() == questions[i][1]){
      answersList[0].push(answer);
    } else {
     answersList[1].push(answer);
    }
  }
  if (answersGiven < questions.length){
   answersGiven +=1; 
  } else {
   print("<h1>Here are your results!</h1>" + "<br>" + answersList[0] + "<br>" + answersList[1]);
   break; 
  }
}

Not sure it's the MOST efficient, but it's probably close. :) Looks good!

1 Answer

Rick de Jong
Rick de Jong
3,305 Points

Thanks Jason Arnold !