美文网首页
[7kyu]Find the smallest integer

[7kyu]Find the smallest integer

作者: 君肄塵 | 来源:发表于2017-07-07 10:54 被阅读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);

相关文章

网友评论

      本文标题:[7kyu]Find the smallest integer

      本文链接:https://www.haomeiwen.com/subject/mojlhxtx.html