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 Arrays Multidimensional Arrays Build a Quiz Challenge – One Solution

Erin White
Erin White
2,383 Points

Why can't I use a while loop for this challenge?

const questions = [
  ["What is your name?", "Erin"]
  ["What color is the sky?", "Blue"]
   ];

let correctAnswers = 0;

while (correctAnswers < questions.length) {
  let question = questions[correctAnswers][0];
  let answers = questions[correctAnswers][1];
  let askQuestion = prompt(question);
  correctAnswers++;
}

I'm writing it this way to bypass the use of the "i" counter or for loop to ensure that I understand how it works without using "i" counter or a for loop as all loop methods(?) are theoretically interchangeable. Otherwise, I'd just be copying Guil's solution without understanding fully what I'm doing.

If you run the code in the console, it throws an error saying that index position "0" in questions[0][0] isn't defined, but obviously it is as the first question in the array "questions".

I hope this makes sense!

Thanks!!!!

You can use whichever loop you like(either a while, do while, for, for of ). But it seems to me that this particular loop that you wrote is ambiguous, especially the correctAnswers variable. Its name suggests that it will hold the number of correct answers but you use it as a counter for the while loop.

1 Answer

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

Hi there, Erin White! The problem here is actually a very tiny error in the initial nested array that you set up. Namely, it's missing a comma between the nested arrays so questions isn't getting properly defined.

You wrote this:

const questions = [
  ["What is your name?", "Erin"]
  ["What color is the sky?", "Blue"]
   ];

But you meant to write:

const questions = [
  ["What is your name?", "Erin"],  // note the comma at the end here
  ["What color is the sky?", "Blue"]
   ];

Hope this helps! :sparkles: