首页 > 其他 > 详细

04.Friend or Foe

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!