V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
supermao
V2EX  ›  程序员

Javscript30 秒, 从入门到放弃

  •  1
     
  •   supermao ·
    hongmaoxiao · 2017-12-24 00:41:37 +08:00 · 5242 次点击
    这是一个创建于 2313 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有意思

    最近很火的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 秒, 从入门到放弃

    第 1 条附言  ·  2017-12-24 07:53:19 +08:00

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

    第 2 条附言  ·  2017-12-24 08:07:12 +08:00

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

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

    17 条回复    2017-12-24 23:20:44 +08:00
    Arnie97
        1
    Arnie97  
       2017-12-24 01:55:27 +08:00 via Android
    全文链接是个用户登录页面
    msg7086
        2
    msg7086  
       2017-12-24 05:53:18 +08:00
    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
        3
    supermao  
    OP
       2017-12-24 07:30:45 +08:00 via iPhone
    @Arnie97 不会吧,难道我贴错了,一会改改
    supermao
        4
    supermao  
    OP
       2017-12-24 07:31:24 +08:00 via iPhone
    @msg7086 666 很优雅
    msg7086
        5
    msg7086  
       2017-12-24 08:12:32 +08:00   ❤️ 2
    另外建议把 Javscript 拼写正确。
    supermao
        6
    supermao  
    OP
       2017-12-24 08:15:58 +08:00
    @msg7086 能再次编辑的已经该过来了,这个实在很不应该,多谢!
    zhidian
        7
    zhidian  
       2017-12-24 09:01:21 +08:00
    我也 fork 了一份, 差不多看完了: https://www.jianshu.com/p/b5b6980b3a65
    supermao
        8
    supermao  
    OP
       2017-12-24 09:09:24 +08:00 via iPhone
    @zhidian 很好玩
    hansnow
        9
    hansnow  
       2017-12-24 12:15:26 +08:00
    Jav Script。。。对不起我想多了。。。
    supermao
        10
    supermao  
    OP
       2017-12-24 12:34:10 +08:00 via iPhone
    @hansnow 哈哈 没发改了,看着真别扭
    bramblex
        11
    bramblex  
       2017-12-24 13:03:21 +08:00
    @msg7086 另外也建议把 JavaScript 的大小写也写正确
    Pastsong
        12
    Pastsong  
       2017-12-24 14:10:04 +08:00
    翻译别人文章的在国内还挺有市场的
    ynyounuo
        13
    ynyounuo  
       2017-12-24 14:10:12 +08:00
    @bramblex 他只是复制了楼主写的 Javscript 而已 - - 你是不是想多了
    supermao
        14
    supermao  
    OP
       2017-12-24 14:28:57 +08:00 via iPhone
    @Pastsong 翻译也是个体力活,就 7、8 个方法写出来也花 2、3 小时。
    yomiko123
        15
    yomiko123  
       2017-12-24 22:11:25 +08:00
    学 java,看这视频教程啊
    http://www.sucaihuo.com/video/271.html
    zhlssg
        16
    zhlssg  
       2017-12-24 22:33:53 +08:00
    用 lodash,没烦恼
    supermao
        17
    supermao  
    OP
       2017-12-24 23:20:44 +08:00 via iPhone
    @zhlssg lodash 比这个效率高多了,但是主要是代码少,优雅啊,看起来更爽,主要作为学习理解用。项目中我基本都是用 lodash
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5075 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:38 · PVG 17:38 · LAX 02:38 · JFK 05:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.