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

Gregory Ledger
Gregory Ledger
6,116 Points

My solution to Challenge 1 Building a Quiz

Um I am so sure this is not the way to do it, but it "works" so, thought?

var gotRight = 0;
var gotWrong = 0;
var result = "";

var questions = [];
questions.push(["Who sang 'When a Man Loves a Woman'?", "percy sledge"]);
questions.push(["Who sang 'Sitting on the Dock of the Bay'?", "otis redding"]);
questions.push(["Who sang 'Sinnerman", "nina simone"]);

var answers = [];
answers.push(prompt(questions[0][0]).toLowerCase());
answers.push(prompt(questions[1][0]).toLowerCase());
answers.push(prompt(questions[2][0]).toLowerCase());

for (var i = 0; i < questions.length; i++) {
  if ((answers[i]) === questions[i][1]) {
      gotRight += 1;

      }else {
      gotWrong += 1
      }
}

result = "You got " + gotRight + " right, and " + gotWrong + " wrong.";

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

1 Answer

Steven Parker
Steven Parker
230,018 Points

Why is it "not the way"? It looks really good to me! :+1:

One minor suggestion: prompt and save the answer inside the loop so it only takes one line instead of three.