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

Please why i am getting this error from the console. "Uncaught TypeError: Cannot read property 'toUpperCase' of null"

// two dimensional array
var setQuestionsAndAnswers = [["What year was 9/11?", "2001"],["What year was Sadam Hussein Killed?", "2006"], ["Who is the 44th President of USA?", "BARACK OBAMA"],["Which year was slavery abolished in USA", "1865"], ["Who invented electricity?","THOMAS  EDISON"], ["What is the tallest mountain in the world?", "EVEREST"]]

// Declared 2 arrays to keep track of questions answered correctly and wrongly
var correctlyAnswered = [];
var wronglyAnswered = [];
var answer = "";


// iterating through the array
  for ( var i = 0; i < setQuestionsAndAnswers.length; i++) {
    answer = prompt(setQuestionsAndAnswers[i][0]);
    answer = answer.toUpperCase(); // im trying to change the "answer" to upper case but it won't let me.

    /** checking if the "answer" is equal to the second index 
    in each of the nested arrays  or not then i add that questions
    to the arrays i declared early respectively" **/
    if ( answer === setQuestionsAndAnswers[i][1]) {
      correctlyAnswered.push(setQuestionsAndAnswers[i][0]);
    }
    else if (answer !== setQuestionsAndAnswers[i][1]) {
      wronglyAnswered.push(setQuestionsAndAnswers[i][0]);
    }
  }

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Edwin,

I have tested the code in my browser's javascript console and it worked. I was able to get to the end and I got a score.

I was even able to check the 2 resulting variables with correct and wrong questions stored.

Vitto

Hello Vitto, It works fine when i run code on safari browser but when i use google chrome with console i get that error. any reason?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Well I have used Chrome when I tested it so it may be something in your settings?

Not sure though...