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

Krasimir Georgiev
Krasimir Georgiev
19,146 Points

My solution.Feedback would be awesome.

var questions = [
  ["To what extinct animals are birds most related to?", "dinosaurs"],
  ["How many months have 28 days?","12"],
  ["How many states are in the U.S.A.?","50"]
];
var answer;
var counter = 0;
function print(message) {
  document.write(message);
}

for(var i = 0; i < questions.length;i++) {  
  answer = prompt(questions[i][0]);
  answer = answer.toLowerCase();
  if (answer == questions[i][1]) {
    questions[i].push("Correct");
    counter++;
  }
}
print("<h2>Number of correct answers: " + counter + "</h2>");
print("<p>Correct Questions:</p>");
for (var a = 0; a < questions.length;a++) {
  if(questions[a].indexOf("Correct") > -1) {
    print(questions[a][0] + "<br>");   
  }
}
print("<p>Incorrect Questions:</p>");
for (var b = 0; b < questions.length;b++) {
  if(questions[b].indexOf("Correct") === -1) {
    print(questions[b][0] + "<br>");   
  }
}

2 Answers

geoffrey
geoffrey
28,736 Points

I edited your code, this way the code is well displayed and more readable. Try to use markdown, It's important, people will be more inclined to give an answer this way. If you want, check this thread, It'll help you. ;-)

Krasimir Georgiev
Krasimir Georgiev
19,146 Points

Thank you, the markdown guide has been very useful. :)

geoffrey
geoffrey
28,736 Points

No problem, I know it can be a bit confusing when you've never used it. At least It was for me !

Good use of push, indexOf and additional loops!

I also used indexOf, but in a slightly different way (you can use it on strings). Here was my solution:

var correct = 0;
var minResponseLength = 5;
var questions = [
  ["What is the largest living land animal?", "african elephant"],
  ["What is the largest living reptile?", "saltwater crocodile"],
  ["What is the largest living fish?", "whale shark"],
  ["What is the largest living bird?", "common ostrich"],
  ["What is the tallest living animal?", "giraffe"]
];

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

// a function to ask the question and return true if answered correctly
function quizQuestion(questions, questionNumber) {
  // get the user's response to the question being asked, convert to lowercase
  var response = prompt(questions[questionNumber][0]).toLowerCase();
  /* return true if the user's response exists within
     or is equal to the actual answer
     AND the response is greater than or equal to the min characters
     (so they can't cheat with a random letter or two)
     - this allows for responses that aren't the full string in the answer */
  return (
    questions[questionNumber][1].indexOf(response) >= 0
    &&
    response.length >= minResponseLength
  )
}

for (var i=0; i<questions.length; i++) {
  if (quizQuestion(questions, i)) {
    correct++;
  }
}

print('<p>Out of ' + questions.length + ' questions, you got ' + correct + ' correct and ' + (questions.length - correct) + '  incorrect.</p>');