FCC_Intermediate Algorithm_Finders Keepers

1.任务及要求

Finders Keepers


写一个 function,它遍历数组 arr,并返回数组中第一个满足 func 返回值的元素。举个例子,如果 arr[1, 2, 3]funcfunction(num) {return num === 2; },那么 find 的返回值应为 2

如果你被卡住了,记得开大招 Read-Search-Ask。尝试与他人结伴编程、编写你自己的代码。

2.我的解法

1 // 就这样
2 function find(arr, func) {
3   return arr.filter(func)[0];
4 }
5 
6 find([1, 2, 3, 4], function(num){ return num % 2 === 0; });

3.发现的其他解法

// CSDN:https://blog.csdn.net/csdn1034595052/article/details/88062239

// CSDN:https://blog.csdn.net/weixin_30776545/article/details/98548008

// 简书:https://www.jianshu.com/p/be7b52ed95a0

 

posted @ 2020-03-16 20:56  yoursatan  阅读(89)  评论(0)    收藏  举报