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

Went crazy with two dimensional arrays, can this be made to work?

edit: I thought I was posting this on the discussion page for the video/challenge but I guess not. Also can't figure out how to delete threads, assuming that's an option. Here's the link to what I'm working on. In short, make a quiz program that stores data in arrays.

Hello! I went browsing through the other questions on here, and it seems my intended solution was a bit different than the others I'm seeing here. I wanted to store a boolean value in the same two dimensional array as my user responses, as the two seemed like related pieces of data. Unfortunately, my solution also isn't working. Every time I run it, I enter all three prompts but only 2 print, and I get a console error saying:

TypeError: answers[i] is undefined | quiz.js:18:19

Here's some of my code, pick it apart:

function print(message) {
  document.write(message);
}
//Questions and Answers
var quizContent = [
  ["what... is your name?", "lancelot"],
  ["What... is your quest?", "holy grail"],
  ["What... is your favorite color?", "blue"]
]
//Hold user answer and boolean to indicate correctness
var answers = [[''],[false]];
//Total number of questions answered correctly
var score = 0;
var i= 0

for (i = 0; i < quizContent.length; i++){
  answers[i][0] = prompt(quizContent[i][0]); //Collect response
  answers[i][0] = answers[i][0].toLowerCase(); //Match case
  //check for equality, update scores
  if (answers[i][0] === quizContent[i][1]){
    answers[i][1] = true;
    score++;
  }
  else {
    answers[i][1] = false;
  }
  //Testing code before moving on
  print(answers[i][0] + answers[i][1]);
}

Eventually I'll probably expand my code out and do it the long way, but I wanted to give a more condensed approach a try. Thanks in advance for any advice.

Edit: There are a few oddities in the code from me trying to isolate issues. For instance, declaring my index variable outside of my for loop was to see if there was something weird with scope. I have some blank data in my answers array to see if I needed to set my data type ahead of time, etc. On both of those, I tested it both with and without but forgot to change it back to it's original state.

1 Answer

Hi, I don't know what is the description for this task. But I think that in your code is problem that you accessing the index of array which not exist. In third cycle when you are looking for "color" then you assign answer to answers array with index of '2'. This index but not exist in your array - you have only '0' and '1'

So, myabe try to extend the answers array on 3 items:

var answers = [[],[],[]];

or another solution is make empty array and fill it with push function

var answers = [];

for (i = 0; i < quizContent.length; i++){
      var answer = prompt(quizContent[i][0]).toLowerCase(); //Collect response
      //check for equality, update scores
      if (answer === quizContent[i][1]){
        answers.push([answer, true]);
        score++;
      }
      else {
        answers.push([answer, false]);
      }
      //Testing code before moving on
      print(answers[i][0] + answers[i][1]);
    }

Huh, maybe I used the get help function wrong. I expected this to be posted under the code challenge that I was searching the community board for. Oh well, I'll update the thread.

Alright, I was able to try some of the advice you gave today. Using .push was the answer I was looking for, didn't realize that I was trying to access an array index before it was created, or even that it was a bad idea. I also found some craziness from me getting my indexes all mixed up. Thanks for the help! My next step will probably be revising the code so that I'm addressing top entry rather than using an index that happens to be going at the same rate.

Revised code:

var quizContent = [
  ["what... is your name?", "lancelot"],
  ["What... is your quest?", "holy grail"],
  ["What... is your favorite color?", "blue"]
]
//Hold user answer and boolean to indicate correctness
var answers = [[],[]];
//Total number of questions answered correctly
var score = 0;
var i= 0

for (i = 0; i < quizContent.length; i++){
  answers[0].push(prompt(quizContent[i][0])); //Collect response
  answers[0][i] = answers[0][i].toLowerCase(); //Match case
  //check for equality, update scores
  if (answers[0][i] === quizContent[i][1]){
    answers[1].push(true);
    score++;
  }
  else {
    answers[1].push(false);
  }
  //Testing code before moving on
  print(answers[0][i] + answers[1][i]);
}