• 请不要在回答技术问题时复制粘贴 AI 生成的内容
supermao
V2EX  ›  程序员

Javscript30 秒, 从入门到放弃

  •  1
     
  •   supermao ·
    hongmaoxiao · Dec 24, 2017 · 6228 views
    This topic created in 3237 days ago, the information mentioned may be changed or developed.

    有意思

    最近很火的github上的库30-seconds-of-code,特别有意思,代码也很优雅。

    1. 能学 es6
    2. 自己翻译,能学英语
    3. 代码很美,很优雅,美即正义
    4. 函数式表达,享受

    arrayGcd

    Calculates the greatest common denominator (gcd) of an array of numbers.

    Use Array.reduce() and the gcd formula (uses recursion) to calculate the greatest common denominator of an array of numbers.

    const arrayGcd = arr =>{
      const gcd = (x, y) => !y ? x : gcd(y, x % y);
      return arr.reduce((a,b) => gcd(a,b));
    }
    // arrayGcd([1,2,3,4,5]) -> 1
    // arrayGcd([4,8,12]) -> 4
    

    计算数组的最大公约数。

    使用Array.reduce()和gcd公式(使用递归)来计算一个数组的最大公约数。

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

    gcd即欧几里德算法,具体不表,自查。这里用到了数组的 reduce 方法,相当简洁,reduce 不太了解的话,看下mdn就明白。

    arrayLcm

    Calculates the lowest common multiple (lcm) of an array of numbers.

    Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.

    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));
    }
    // arrayLcm([1,2,3,4,5]) -> 60
    // arrayLcm([4,8,12]) -> 24
    

    计算一个数组的最小公倍数。

    使用Array.reduce()和lcm公式(使用递归)来计算一个数组的最大公约数。

    ➜  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
    

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

    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
    

    返回数组中最大的值。

    使用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.

    const arrayMin = arr => Math.min(...arr);
    // arrayMin([10, 1, 5]) -> 1
    

    返回数组中最小的值。

    使用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()干的事,没啥可说的了。

    全文太长,放个全文链接吧:
    Javscript30 秒, 从入门到放弃

    Supplement 1  ·  Dec 24, 2017

    全文地址贴错了,放一个新的: javscript30秒, 从入门到放弃

    Supplement 2  ·  Dec 24, 2017

    放github的issues上了,有更好的阅读体验。
    个人翻译水平有限,欢迎大家在issues上批评指正。 javscript30秒, 从入门到放弃

    还有微信公众号也有,着急在昨天晚上12点前发,没认真排版,还是issues的阅读体验最佳。

    17 replies  •  2017-12-24 23:20:44 +08:00
    Arnie97
    Arnie97
    Dec 24, 2017 via Android
    1
    全文链接是个用户登录页面
    msg7086
    msg7086
    Dec 24, 2017
    2
    Ruby 可以这么玩

    class Fixnum; def gcd(y); y == 0 ? self : y.gcd(self % y) end; end # 2.3
    class Array; def gcd; uniq.reduce{ |a, b| a.gcd(b) } end; end

    [12,9,24].gcd
    => 3
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    3
    @Arnie97 不会吧,难道我贴错了,一会改改
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    4
    @msg7086 666 很优雅
    msg7086
    msg7086
    Dec 24, 2017❤️2
    5
    另外建议把 Javscript 拼写正确。
    supermao
    supermao
    OP
    Dec 24, 2017
    6
    @msg7086 能再次编辑的已经该过来了,这个实在很不应该,多谢!
    zhidian
    zhidian
    Dec 24, 2017
    7
    我也 fork 了一份, 差不多看完了: https://www.jianshu.com/p/b5b6980b3a65
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    8
    @zhidian 很好玩
    hansnow
    hansnow
    Dec 24, 2017
    9
    Jav Script。。。对不起我想多了。。。
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    10
    @hansnow 哈哈 没发改了,看着真别扭
    bramblex
    bramblex
    Dec 24, 2017
    11
    @msg7086 另外也建议把 JavaScript 的大小写也写正确
    Pastsong
    Pastsong
    Dec 24, 2017
    12
    翻译别人文章的在国内还挺有市场的
    ynyounuo
    ynyounuo
    Dec 24, 2017
    13
    @bramblex 他只是复制了楼主写的 Javscript 而已 - - 你是不是想多了
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    14
    @Pastsong 翻译也是个体力活,就 7、8 个方法写出来也花 2、3 小时。
    yomiko123
    yomiko123
    Dec 24, 2017
    15
    学 java,看这视频教程啊
    http://www.sucaihuo.com/video/271.html
    zhlssg
    zhlssg
    Dec 24, 2017
    16
    用 lodash,没烦恼
    supermao
    supermao
    OP
    Dec 24, 2017 via iPhone
    17
    @zhlssg lodash 比这个效率高多了,但是主要是代码少,优雅啊,看起来更爽,主要作为学习理解用。项目中我基本都是用 lodash
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1411 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 20:49 · PVG 04:49 · LAX 12:49 · JFK 15:49
    ♥ Do have faith in what you're doing.