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 Object-Oriented JavaScript (2015) Practice Project Project Overview

Trouble pushing to Array.

With the code below my array is empty. I am unable to figure out why.

// Question.js

function Question(question) {
    this.question = question;
    return question;
}

Question.prototype.toHTML = function(question) {
    var htmlString = question;
    return htmlString;
};

// Quiz.js

function Quiz() {
    this.questions = [];
    this.currentQuestionIndex = 0;
    console.log(this.questions);
}

// Adds new question to array
Quiz.prototype.add = function(newQuestion) {
    this.questions.push(newQuestion);
};

// App.js

// Create quiz
var quiz = new Quiz();

// Create Questions
var question1 = new Question('What is 2 + 2?');
var question2 = new Question('What is the capitol of the United States?');

// Add Questions
quiz.add(question1);
quiz.add(question2);

1 Answer

Why do you think it's empty?

It's empty where you have the console.log statement but I don't think it's empty by the end of the code.

Try running console.log(quiz.questions); as your last line and you should see that it's populated.

Thanks! It isn't empty. You also answered the question I asked before this.