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

Julian Garcia
Julian Garcia
18,380 Points

I am receiving this error : undefined is not an object

I am trying this challenge but I am receiving undefined is not an object (qas[res[j]]).

function print(message) {
  document.write(message+'<br>');
}

function promptMe(qa1){
  ans = prompt(qa1[0]);
  if( ans === qa1[1]){
    prompt("Your are right!!");
    return 1;
  }
  return 0;
}

function quizMe(qas){
  correct_ans = []
  for(var i=0;i< qas.length ; i++) {
    res = promptMe(qas[i]);
    if( res !== 0) {
      correct_ans.push(i);
    }
  }
  return correct_ans;
}

function main_f() {
  var qas = [ ["What is the main tag for a html page?", "html"],
          ["How many States has the Unites States?", "50"],
          ["Does exist Alien?", "Yes"]];
  var res = quizMe(qas);

  print("Right Answered questions");
  for(var i=0; i < res.length ; i++) {
    print(qas[res[i]][0]);    
    print(qas[res[i]][1]);
  }
  print("Wrong Answered questions");
  var count_match=0;
  for(var j=0; j < qas.length ; j++) {
    if(res.indexOf(j)===-1){
      print(qas[res[j]][0]); //<<---Undefined is not an object(evaluating 'qas[res[j]]')   
      print(qas[res[j]][1]);
    }
    }    

}

main_f()

1 Answer

Julian Garcia
Julian Garcia
18,380 Points

Solved. I was trying to access

print(qas[res[j]][0]);

but it should be :

print(qas[j][0]);