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

Sara Masek
seal-mask
.a{fill-rule:evenodd;}techdegree
Sara Masek
Front End Web Development Techdegree Student 11,513 Points

Prompt repeating answer and correctAnswers aren't incrementing

Hi all, I ended up having to copy most of Guil's code but I'm not getting any answers returned to me - on the web page it always displays as "0" for the correct answers, even if I give the correct answers. It seems to not be incrementing, and I can't figure out why. Another issue is when you enter an answer into the prompt, the box returns the answer you gave, while still displaying the prompt. So in other words two prompts show up per question, which I've never seen before. If anyone's able to find the error I would really appreciate it, since I've double checked my code a ton already an am at a loss.

let quizQuestions = [
  ['What was Marios orginal name?'], ['jumpman'],
  ['In Ocarina of Time, what is the name of your horse?'], ['Epona'],
  ['How many releases has Wind Waker had?'], ['two']
];

let correctAnswers = 0;

for (let i = 0; i < quizQuestions.length; i++) {
  let question = quizQuestions[i][0];
  let answer = quizQuestions[i][1];
  let response = prompt(question);


  if (response === answer) {

    correctAnswers++;
  }

}

let html = `
  <h1>You got ${correctAnswers} question(s) correct</h1>
`;

document.querySelector('main').innerHTML = html;

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Sara Masek! Looks like you're doing well to me so far. The problem here is in the initial setup of the array with nested arrays.

Currently you have:

let quizQuestions = [
  ['What was Marios orginal name?'], ['jumpman'],
  ['In Ocarina of Time, what is the name of your horse?'], ['Epona'],
  ['How many releases has Wind Waker had?'], ['two']
];

This is close, but not really what you're after. In this case quizQuestions[0][0] is the first question but quizQuestions[0][1] is not the corresponding answer. In short, you've made a few too many arrays here.

let quizQuestions = [
  ['What was Marios orginal name?', 'jumpman'], // the first element in the inner array is the question, the second is the answer
  ['In Ocarina of Time, what is the name of your horse?', 'Epona'],
  ['How many releases has Wind Waker had?', 'two']
];

Hope this helps! :sparkles: