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 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
220,853 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.