学习别人写的质数#JS_codewar_1

题目:

创建一个名称为divisor的方法,传入整数后,如果是质数则返回“xx is integer”,如果不是则返回除1以外的除数,以数组的形式。

Description:
Create a function named divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime' (use Either String a in Haskell).

我的方法:

function divisors(integer) {
    let box = [];
    let half = integer / 2;
    for (i=2; i<=half; i++) {
        let dd = integer % i;
        if (dd == 0) {
            box.push(i);
        }
    }
    if (box.length !== 0) {
        return box;
    } else {
        return integer + ' is prime'
    }
}

别人的方法:

function divisors(integer) {
  var res = []
  for (var i = 2; i <= Math.floor(integer / 2); ++i) if (integer % i == 0) res.push(i);
  return res.length ? res : integer + ' is prime'
};

我的感受:

真的,别人看上去特别智能,我的看上去特别憨厚老实。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,029评论 0 23
  • 好久没有字斟句酌地读书了,知道读书会的主题开始拿起哈姆雷特,真是不得不全神贯注才能领略妙处,一周不过读了少半,经典...
    urna阅读 218评论 0 0
  • 泪水打湿了整个秋天 空空的行囊在风中 飘荡了半个世纪 不再模糊 捡起丢失的春天 天不会老, 地不会荒 相对论老人在...
    晚风吹日月阅读 222评论 0 6
  • 老妈在微信上传来一篇文章,大概是讲买东西就要买贵一点的,廉价意味着退而求其次。 可是,贵与便宜是一个相...
    静雅_李阅读 160评论 0 0