Vinny
Vinny

Reputation: 5

TypeError: Cannot read property .... of undefined

I've been trying to track down for the past hour. When I put it in my IDE, no errors come up for intellisense so I don't think there are any typos but when I run the js file in chrome or in node I receive the following error:

"TypeError: Cannot read property displayQuestion of undefined"

Here is my code below, all I am trying to do is have it select one of the random questions, prompt the user asking for input and the question is asked in the console. To which it then compares the input to the correct answer.

(function() {

function Question(question, answers, correct){
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }

Question.prototype.displayQuestion = function(){
    console.log(this.question);

    for(i in this.answers){
        console.log(this.answers[i]);
    }
}

Question.prototype.checkAnswer = function(answer){
    if(answer === correct){
        console.log("Great job! Correct.");
    }
    else{
        console.log("Incorrect. Please try again.");
    }

}

let q1 = Question("Will I eat dinner?", ["Yes", "No"], 0);
let q2 = Question("What is my name?", ["John", "James", "Charles"], 2);
let q3 = Question("Will I go to sleep?", ["Yes", "No"], 1);

let questions = [q1, q2, q3]

let n = Math.floor(Math.random() * questions.length);
questions[n].displayQuestion();
var answer = prompt('Please select the correct answer.');
questions[n].checkAnswer(answer);
})();

Upvotes: 0

Views: 2832

Answers (3)

Jai
Jai

Reputation: 91

You missed on the constructor . Try adding new

let q1 = new Question("Will I eat dinner?", ["Yes", "No"], 0);

Upvotes: 0

sparrow
sparrow

Reputation: 1957

For function Question, you're trying to construct a class. You need to call new on your Question calls for it to return the Question object.

let q1 = Question("Will I eat dinner?", ["Yes", "No"], 0);
typeof q1 // 'undefined'
let q2 = new Question("What is my name?", ["John", "James", "Charles"], 2);
typeof q2 // 'object'

For background, look at the difference between invoking a function and calling new on that function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

Upvotes: 0

ellipsis
ellipsis

Reputation: 12152

Use the new keyword for creating new instances

(function() {

function Question(question, answers, correct){
        this.question = question;
        this.answers = answers;
        this.correct = correct;
    }

Question.prototype.displayQuestion = function(){
    console.log(this.question);

    for(i in this.answers){
        console.log(this.answers[i]);
    }
}

Question.prototype.checkAnswer = function(answer){
    if(answer === correct){
        console.log("Great job! Correct.");
    }
    else{
        console.log("Incorrect. Please try again.");
    }

}

let q1 = new Question("Will I eat dinner?", ["Yes", "No"], 0);
let q2 =  new Question("What is my name?", ["John", "James", "Charles"], 2);
let q3 = Question("Will I go to sleep?", ["Yes", "No"], 1);

let questions = [q1, q2, q3]

let n = Math.floor(Math.random() * questions.length);
questions[n].displayQuestion();
var answer = prompt('Please select the correct answer.');
questions[n].checkAnswer(answer);
})();

Upvotes: 2

Related Questions