Friendship "class"

by ronilan

JavaScript

// this is the Friendship "class".
function Friendship() { 
    this.pair = [];
}

/**
    makeFriend method takes 2 String parameters and makes them "friend" of each other.  
    Note: The order of names does not matter
     
    @param {string} name1, {string} name2
	@return null;    
*/

Friendship.prototype.makeFriend = function(name1, name2){
    this.pair.push({name1: name1, name2: name2});
    return null;
}

/**
    unmakeFriend method takes 2 String parameters and makes them no longer friends of each other.  
    Note: The order of names does not matter
     
    @param {string} name1, {string} name2
	@return null;    
*/

Friendship.prototype.unmakeFriend = function(name1, name2){
    
    var i,
        max = this.pair.length;
    
    for (i = 0; i < max; i++) {
        if (
            (this.pair[i].name1 === name1 && this.pair[i].name2 === name2) || 
            (this.pair[i].name2 === name1 && this.pair[i].name1 === name2)
        ) {
            this.pair.splice(i,1);
            break;
        }
    }
    
    return null;
}

/*
    getDirectFriends method takes a single argument (name) and returns all the immediate "friends" of that name as an array of strings
    
    For example, A & B are friends, B & C are friends and C & D are friends.
    getDirectFriends(B) would return A and C
    getDirectFriends(D) would return C
    Note: It should not return duplicate names

    @param {string} name
	@return {Array};   
*/

Friendship.prototype.getDirectFriends = function(name){

    var i,
        max = this.pair.length,
        result = [];

    for (i = 0; i < max; i++) {
        if (this.pair[i].name1 === name && result.indexOf(name) === -1) {
            result.push(this.pair[i].name2) 
        }
        if (this.pair[i].name2 === name && result.indexOf(name) === -1 ) {
            result.push(this.pair[i].name1) 
        }        
    }
    
    return result;
    
}
/**
    getIndirectFriends method takes a single argument (name) and returns...