[7kyu]Find the smallest integer
2017-07-07 本文已影响13人
君肄塵
该算法题来自于 codewars【语言: javascript】,翻译如有误差,敬请谅解~
- 任务
- 找出数组中最小的元素,并返回。
- 例如:
[34, 15, 88, 2] // 返回 2
[34, -345, -1, 100] // 返回 -345
- 解答
- 其一
const findSmallestInt = arr => arr.sort((a,b)=>a-b)[0];
- 其二
const findSmallestInt = arr => Math.min(...arr);
- 其三
const findSmallestInt = arr => Math.min.apply(null, args);
- 其四
const findSmallestInt = arr => arr.reduce((prev, curr) => prev < curr ? prev : curr);