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
alex gwartney
8,849 Pointsprototype function not working?
So im trying to get my prototype function to run but for some reason when i go to run the program its telling me that the prototype function is not a function. Could some one take a look and see what im doing incorrectly?
function questions(questions){
this.questions = questions;
}
questions.prototype.addquestion = function(){
var questions = [];
var ask = questions.push(this.questions);
for(i=0; i<ask; i++){
console.log(ask[0]);
}
}
questionone = new questions("question one");
questionone.addquestion();
1 Answer
justindennison
15,922 PointsHey Alex, You are definitely trying to apply your new found skills. I like that you dove right into adding functions to an object prototype. However, there are a few things that we need to look at: <<I have commented your code as well as supplied a solution...>>
Your code with comments
/*You should consider creating an object that can be considered as a single entity. Instead of questions, maybe question. If you want a collection of questions, then create an array of those objects... This makes things a little easier to reason about as well as manipulate. Moreover, consider choosing meaningful and different names for objects, function parameters, and properties or functions associated with an object.*/
function questions(questions){
this.questions = questions;
}
/*The naming conventions here are not incorrect and this will work if you are just trying to create a question object. However, the array of questions inside of an object makes things difficult to reason about.*/
questions.prototype.addquestion = function(){
var questions = [];
var ask = questions.push(this.questions);
/*the push method of arrays returns the new length of the array after the added element*/
for(i=0; i<ask; i++){
console.log(ask[0]); //This will be an undefined quantity because of ask does not contain a collection type
}
}
questionone = new questions("question one");
questionone.addquestion();
My proposed alterations --> These are not required changes, but may provide some clarity in what I think you are trying to accomplish
//Define a question object prototype through a constructor function
function Question(text, answer){
this.text = text;
this.answer = answer;
}
//Create a QuestionBank object that encapsulates the behavior of the array that is storing your question objects
function QuestionBank(){
this.questions = []; //A question bank is an empty collection upon creation.
}
QuestionBank.prototype.addQuestion = function(question){
this.questions.push(question); //Adds the question to the question bank
}
//Print all of the questions in the question bank
QuestionBank.prototype.printBank = function(){
for(var i = 0; i < this.questions.length; i++){
console.log("Question " + i +": "+ this.questions[i].text + "\n");
}
}
/*How would you use this?*/
var q1 = new Question("What is your favorite color?", "Blue");
var qBank = new QuestionBank();
qBank.addQuestion(q1);
qBank.printBank();
Hopefully that helps! Happy Coding!