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

Code review: JavaScript constructors and prototpyes

// here i'm creating a new constructor for a user object
function User(first_name, last_name, email, password, friends, favorites) {
    this.first_name = first_name;
    this.last_name = last_name;
    this.email = email;
    this.password = password;
    this.friends = [];
    this.favorites = [];
}

// this function adds a new friend to the friends array
User.prototype.add_friend = function(friend) {
    this.friends.push(friend);
}

// this function deletes a friend from the friends array 
User.prototype.delete_friend = function(friend) {
    this.friends.splice(this.friends.indexOf(friend), 1);
}

// this function adds a favorite to the favorites array
User.prototype.add_favorite = function(favorite) {
    this.favorites.push(favorite);
} 

// this function deletes a favorite from the favorites array
User.prototype.delete_favorite = function(favorite) {
    this.favorites.splice(this.favorites.indexOf(favorite), 1);
}

var user = new User("Andrew", "Chalkley", "andrew@teamtreehouse.com", "secretpassword");

I'm mainly concerned about the 'delete' functions that are supposed to splice a friend or favorite from it's array.