作家
登录

JavaScript30秒, 从入门到放弃

作者: 来源: 2017-12-25 14:05:13 阅读 我要评论

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

    JavaScript30秒, 大年夜入门到放弃

    1. 能学es6
    2. 本身翻译,能学英语
    3. 代码很美,很优雅,美即公理
    4. 函数式表达,享受

    lcm 算法用到了前面的 gcd 算法,关键点是两个数的最大年夜公约数和最小公倍数的乘积正好就是这两个数的乘积。

    1. const arrayMin = arr => Math.min(...arr); 
    2. // 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.

    1. const arrayMax = arr => Math.max(...arr); 
    2. // arrayMax([10, 1, 5]) -> 10 

    返回数组中最大年夜的值。

    [].concat(...arr.map(fn)) 用空数组把 map 运算产生的数组进行 … 扩大运算值拼接成结不雅数组返回。

    应用 Math.max() 和 ES6 的扩大运算符 … 返回数组中最大年夜的值。

    1. ➜  code cat arrayMax.js 
    2. const arrayMax = arr => Math.max(...arr); 
    3.  
    4. console.log(arrayMax([10, 1, 5])); 
    5. ➜  code node arrayMax.js 
    6. 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 的扩大运算符 … 返回数组中最小的值。

    1. ➜  code cat arrayMin.js 
    2. const arrayMin = arr => Math.min(...arr); 
    3.  
    4. console.log(arrayMin([10, 1, 5])); 
    5. ➜  code node arrayMin.js 

    实际上就是 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.

    1. const chunk = (arr, size) => 
    2.  Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * 

        推荐阅读

        NodeJs爬虫抓取古代典籍,共计16000个页面心得体会总结及项目分享

      【限时免费】岁尾最强一次云计算大年夜会,看传统、社区、互联网企业若何碰撞? 之前研究数据,零零碎散的写过一些数据抓取的爬虫,不过写的比较随便。有很多处所如今看起来并不是很合理 这>>>详细阅读


      本文标题:JavaScript30秒, 从入门到放弃

      地址:http://www.17bianji.com/lsqh/40136.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)