简体   繁体   中英

8.12.5: Mutual Friends CodeHS

Here's the given problem:

Write a program that prints the mutual friends between two people.

You should create two sets, one for each person, and add friends (strings) to each set representing the friends of that person.

Then, fill in the mutualFriends function that takes the two people as parameters and returns a new set that includes their mutual friends.

Print out the set of mutual friends

Hints There are multiple ways you can do this. Not all of the hints apply to each method.

You can loop over the elements in a set like this:

for (item of your_set) {
   println(item);
}

You can use the set methods union or intersect like this:

// returns a set which is the union of a and b
var aUnionB = a.union(b);

// returns a set which is the intersection of a and b
var aIntersectB = a.intersect(b);

This is the code I did:

function start(){
    // Create your friend lists here
    var friendOne = new Set ("Timmy", "John", "Greg", "Sophia", "Kate");
    var friendTwo = new Set ("Nate", "John", "Kate", "Timmy", "Lee");
    mutualFriends(friendOne, friendTwo);
} 

/* This function takes two people, a, b
* which are sets of their friends.
* It returns a new set that holds the
* mutual friends of a and b.
*/
function mutualFriends(a, b) {
// Write this function here
   var friends = [];
   for (var items in a && b) {
   if(a.contains(items in b)){
       friends.add(items);
   }else if(b.contains(items in a)){
       friends.add(items);
   }else{
       println("Not working.");
    }
}
println("A and B's mutual friends are: " + friends);
return friends;
}

Instead, my code doesn't display the mutual friends and ignores the if statements. Any help would be appreciated, thanks!

There are some syntax problems in your original codes.

(1) If you want to get the mutual friends from two lists, you only need to compare once (say compare listA with listB), there is no need to compare listB with listA again.

(2) please use "has" to check whether the listB has friend in listA

Please find below the codes which demonstrate what is needed:

<script>

    let friendOne = new Set (["Timmy", "John", "Greg", "Sophia", "Kate"]);
    let friendTwo = new Set (["Nate", "John", "Kate", "Timmy", "Lee"]);
    let mutualfriend=new Set ();

    for (let item of friendOne){
        if (friendTwo.has(item)){
            mutualfriend.add(item);
        }
    }



    document.write("The mutual friends are:<br>");

    for (let mfriend of mutualfriend){
        document.write(mfriend);
        document.write("<br>");
    }


</script>

You may use union / intersect to further modify your codes. Please try working on it if necessary.

I figured out how to do it. This only works in codeHS.

function start(){
    // Create your friend lists here
    var friendOne = new Set ();
    var friendTwo = new Set();
    //Friends of friendOne
    friendOne.add("Timmy");
    friendOne.add("John");
    friendOne.add("Greg");
    friendOne.add("Sophia");
    friendOne.add("Kate");
    println(friendOne);
    //Friends of friendTwo
    friendTwo.add("Nate");
    friendTwo.add("Kate");
    friendTwo.add("John");
    friendTwo.add("Timmy");
    friendTwo.add("Lee");
    println(friendTwo);

    mutualFriends(friendOne, friendTwo);
}

/* This function takes two people, a, b
 * which are sets of their friends.
 * It returns a new set that holds the
 * mutual friends of a and b.
*/

function mutualFriends(a,b){
    var friends = new Set();
    for(var item of a){
        if (b.contains(item)){
            friends.add(item);
        }
    }
    for(var item of b){
        if (a.contains(item)){
            friends.add(item);       
        }
    }
    println("Mutual friends are: " + friends);
    return friends;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM