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

Mark Packard
PLUS
Mark Packard
Courses Plus Student 937 Points

I'm only able to get the last question of my quiz to provide the correct answer.

I haven't finished the project yet, at this point I'm just trying to print the word 'yay' if the question is correct and 'no' if the question is incorrect. It will print out 'yay' if I answer the third question correctly but no matter how I answer the first two questions it prints out 'no.' Any suggestions? Thank you!

var questionArray = [
  ["What is Sean Daley's MC name?", "Slug"],
 ["What is Anthony Davis's DJ name?", "Ant"],
 ["What do these two musicians call their rap duo?", "Atmosphere"]
  ];

/*function print(message) {
  document.write(message);

}
*/
console.log(questionArray[1][1]);
  for (var i = 0; i < questionArray.length; i += 1){
var questions = prompt(questionArray[i][0]);
}
 for (var i = 0; i < questionArray.length; i+=1)
{
if (questions === questionArray[i][1]){
  document.write("yay ");
}
else document.write("no ");
}

3 Answers

Michael Lauridsen
Michael Lauridsen
10,321 Points

Hi Mark,

I haven't seen the course so I'm not sure this is the way they're trying to show you, and I'm still a beginner so take this with a grain of salt. But I saw two bugs in your code:

In your last If/Else condition you forgot to encapsulate some of your code. (Specifically after your else, and you forgot to close the for loop);

When you have your var questions, you have to think about how you are saving it in the variable. I used the .push() method, to save the array answers in a empty array. I did this since you don't need to know the questions, to know if the answer is correct, as long as they are checked at the same index.

Also, instead of using two for loops, you can simply just use one in this case.

var questionArray = [
  ["What is Sean Daley's MC name?", "Slug"],
 ["What is Anthony Davis's DJ name?", "Ant"],
 ["What do these two musicians call their rap duo?", "Atmosphere"]
  ];

var questions = [];
for (var i = 0; i < questionArray.length; i++) {
    questions.push(prompt(questionArray[i]));

    if (questions[i] === questionArray[i][1]) {
        document.write("yay ");
    } else {
        document.write("no ");
    }
}
Mark Packard
PLUS
Mark Packard
Courses Plus Student 937 Points

That helped a lot, thank you so much for your help! I still can't get it to work perfectly but your method did fix the problem I was having.

Michael Lauridsen
Michael Lauridsen
10,321 Points

If you're interested, it can be done a tad more simple actually. You don't need to store the answers in the array, if you're not going to use them later (just to make sure I didn't create any confusion with the weird questions array: I thought you had to save the typed answers somewhere, which got put into the questions array. So a more fitting name would've been answer). So since we're looping through the questions, we can just check the prompt answer. I hope this helps!

var questionArray = [
  ["What is Sean Daley's MC name?", "Slug"],
 ["What is Anthony Davis's DJ name?", "Ant"],
 ["What do these two musicians call their rap duo?", "Atmosphere"]
  ];

for (var i = 0; i < questionArray.length; i++) {
    var answer = prompt(questionArray[i][0]);
    if (answer === questionArray[i][1]) {
        document.write("yay ");
    } else {
        document.write("no ");
    }
}