码迷,mamicode.com
首页 > 其他好文 > 详细

04.Friend or Foe

时间:2017-07-25 12:33:50      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:list   with   filter   length   and   ring   dnv   dex   let   

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

标签:list   with   filter   length   and   ring   dnv   dex   let   

原文地址:http://www.cnblogs.com/chengnanbei/p/7233223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!