test for similarities in friends

PHOTO EMBED

Tue Sep 07 2021 13:55:26 GMT+0000 (Coordinated Universal Time)

Saved by @chisomloius ##class ##oop ##javascript ##objects

/*
In this challenge, you will test your knowledge of objects.

THE PROBLEM:
Social networks are key part of people's online experience. Platforms like
facebook, tiktok, and linkedin etc allow people to connect online either for
professional or casual friendships.

Since it's relevatively easy to connect with friends online, most people often
realize they are friends with some other person through indirect connections. i.e
Chuks and Lola are friends because Adamu is a friend of Chuks and Lola.

Chuks <--> Adamu <--> Lola.

YOUR CHALLENGE:
Complete the code below to establish friendship between two persons, and also
determine if two people are friends. Two people are friends if and only if (iff)
 -  They are direct friends.
 -  They are separated by one degree of friendship i.e

    Given: Chuks <--> Adamu <--> Lola

       Chuks and Lola are friends because

       Chuks is friends with Adamu

       And Adamu is friends with Lola


    Given: Chuks <--> Adamu <--> Lola <--> Sade

       Chuks and Sade are NOT friends because they are separated by more
       than one degree of friendship.

*/

// Constructor function for a Person.
// DO NOT MODIFY
function Person(name) {
    this.name = name;
    this.friends = [];

    // TASK1: Add a method friendCount that returns the number of friends a person has
    // YOUR CODE HERE
    this.friendCount = function(){
        return this.friends.length
    }
}

// Establishes a friendship between two persons
// HINT: friendship is symmetrical.
// i.e if a A is a fiend of B, then B is also a friend  of A
function connect(personA, personB) {
 // TASK 2: Connect two the users are friends
 // YOUR CODE HERE
    this.personA = personA;
    this.personB = personB;
    personA.friends.push(personB);
    personB.friends.push(personA)

}

// Rturn true if and only if (iff)
// 1) They are direct friends
//           OR
// 2) They are separated by one degree of friendship
function friendship(personA, personB) {
    let friends = false;

    // TASK 3: Determine if two users are friends
    // YOUR CODE HERE
    for(let i in personA.friends){
        if(personA.friends[i] === personB)
            return (friends = true)
    }
    for (let i=0; i < personA.friends.length; i++){
        for(let j = 0; j < personA.friends[i].friends.length; j++){
            if(personA.friends[i].friends[j] === personB){
                    return (friends = true)
            }   
        }

    }
    return friends
}

// THIS IS FOR YOUR TESTING ONLY.
const kogi = new Person('kogi')
const fela = new Person('fela')
const asa = new Person('asa')
const david = new Person('david')
const kanu = new Person('kanu')

connect(kogi, fela)
connect(kogi, asa)
connect(asa, fela)
connect(asa, david)
connect(david, kanu)

console.log('kogi and fela = ' + friendship(kogi, fela) )   // should print true
console.log('fela and kogi = ' + friendship(fela, kogi) )   // should print true
console.log('kogi and david = ' + friendship(kogi, david) ) // should print true
console.log('kogi and kanu = ' + friendship(kogi, kanu))  // should print false
console.log(kogi.friendCount()) // should print 2
content_copyCOPY

www.edconnect.ng