码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript算法相关

时间:2017-05-24 23:55:39      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:map   number   https   index   tps   rip   code   apply   null   

1. 不使用循环,创建一个长度为100的数组,并且每个元素的值等于它的下标?

Array.apply(null, {length: N}).map(Function.call, Number);
Array.apply(0,Array(100)).map(function(item,index){return index})

https://segmentfault.com/q/1010000004872068/a-1020000004873461

2. Javascript 求100以内的质数

var prime = function(len){
    var i,j;
    var arr = [];
     
  for(i = 1; i < len; i++){
    for(j=2; j < i; j++){  
      if(i%j === 0) {
         break;
      }
    }
     
    if(i <= j && i !=1){
      arr.push(i);
    }
    
  }
  return arr;
};
console.log(prime(100));

 

JavaScript算法相关

标签:map   number   https   index   tps   rip   code   apply   null   

原文地址:http://www.cnblogs.com/Chen-XiaoJun/p/6901319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!