- ➜ code cat arrayLcm.js
- const arrayLcm = arr => {
- const gcd = (x, y) => (!y ? x : gcd(y, x % y));
- const lcm = (x, y) => x * y / gcd(x, y);
- return arr.reduce((a, b) => lcm(a, b));
- };
- console.log(arrayLcm([1, 2, 3, 4, 5]));
- console.log(arrayLcm([4, 8, 12]));
- ➜ code node arrayLcm.js
- 60
- 24

- 能学es6
- 本身翻译,能学英语
- 代码很美,很优雅,美即公理
- 函数式表达,享受
lcm 算法用到了前面的 gcd 算法,关键点是两个数的最大年夜公约数和最小公倍数的乘积正好就是这两个数的乘积。
- const arrayMin = arr => Math.min(...arr);
- // arrayMin([10, 1, 5]) -> 1
arrayMax
Returns the maximum value in an array.
Use Math.max() combined with the spread operator ( ... ) to get the maximum value in the array.
- const arrayMax = arr => Math.max(...arr);
- // arrayMax([10, 1, 5]) -> 10
返回数组中最大年夜的值。
[].concat(...arr.map(fn)) 用空数组把 map 运算产生的数组进行 … 扩大运算值拼接成结不雅数组返回。
应用 Math.max() 和 ES6 的扩大运算符 … 返回数组中最大年夜的值。
- ➜ code cat arrayMax.js
- const arrayMax = arr => Math.max(...arr);
- console.log(arrayMax([10, 1, 5]));
- ➜ code node arrayMax.js
- 10
实际上就是 Math.max() 干的事,没啥可说的了。
arrayMin
Returns the minimum value in an array.
Use Math.min() combined with the spread operator ( ... ) to get the minimum value in the array.
返回数组中最小的值。
应用 Math.min() 和 ES6 的扩大运算符 … 返回数组中最小的值。
- ➜ code cat arrayMin.js
- const arrayMin = arr => Math.min(...arr);
- console.log(arrayMin([10, 1, 5]));
- ➜ code node arrayMin.js
- 1
实际上就是 Math.min() 干的事,没啥可说的了。
Chunks an array into smaller arrays of a specified size.
Use Array.from() to create a new array, that fits the number of chunks that will be produced. Use Array.slice() to map each element of the new array to a chunk the length of size . If the original array can't be split evenly, the final chunk will contain the remaining elements.
- const chunk = (arr, size) =>
- Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i *
推荐阅读
NodeJs爬虫抓取古代典籍,共计16000个页面心得体会总结及项目分享
【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞? 之前研究数据,零零碎散的写过一些数据抓取的爬虫,不过写的比较随便。有很多处所如今看起来并不是很合理 这>>>详细阅读
地址:http://www.17bianji.com/lsqh/40136.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示