1. 程式人生 > >04.Friend or Foe

04.Friend or Foe

fun lis dex == lov 字符串 word ace nds

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