首页 > 代码库 > 04.Friend or Foe

04.Friend or Foe

Make a program that filters a list of strings and returns a list with only your friends name in it.

If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours!

Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]

简单来说就是找出数组中所有四个字符的字符串


function friend(friends){
//your code here
var arr=friends.filter(function(x,index){
return x.length==4;
})

return arr;
}

测试数据:

friend(["Ryan", "Kieran", "Mark"]), ["Ryan", "Mark"];

friend(["Ryan", "Jimmy", "123", "4", "Cool Man"]), ["Ryan"];
friend(["Jimm", "Cari", "aret", "truehdnviegkwgvke", "sixtyiscooooool"]), ["Jimm", "Cari", "aret"];
friend(["Love", "Your", "Face", "1"]), ["Love", "Your", "Face"];

最佳答案

function friend(friends){ return friends.filter(n => n.length === 4) }

04.Friend or Foe