N-ZOO / everycode

前端每日一练
163 stars 18 forks source link

[js]不使用loop,创建一个长度为100的数组 #19

Open VaJoy opened 8 years ago

VaJoy commented 8 years ago

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

从某站看到的一道淘宝笔试题,挺有趣的,大家有兴趣可以做一做 :smile: 建议自行完成哦别搜索,不然就直接找到答案了

mumuy commented 8 years ago

var arr = new Array(100); arr = arr.map(function(item, index) { return index; });

VaJoy commented 8 years ago

@mumuy 恭喜中陷阱,你可以在浏览器运行下哦,会得到一个元素全是undefined的数组 :smile:

chengnuo commented 8 years ago

用递归

yuersmall commented 8 years ago

看来我确实弱爆了

tianzi77 commented 8 years ago

Array(100).join(",").split(",").map(function(key,index){return index;})

VaJoy commented 8 years ago

收集下A神的:

Array.apply(null, {length: 10}).map(Number.call, Number) 

我改一下:

Object.keys(Array.apply(null, {length: 10}))
Cxiaohuiyang commented 8 years ago

``Array.from({length:100} , function(value, index){ return index })

Keith-CY commented 8 years ago

var arr = Array.from({length:100}, (v,k) => k); arr [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

sevenCon commented 8 years ago

Object.keys(Array(100).toString().split(","))

anilpixel commented 8 years ago
var list = [];

(function next(i) {
  if (i < 100) {
    list.push(i);
    next(++i);
  }
}(0));
Yoomin233 commented 7 years ago
function genArr(i, arr){
    if(i < 10){
        arr[i] = i++
        return genArr(i, arr);
    } else {
        return arr;
    }
}
var arr = genArr(0, [])
shaonq commented 7 years ago

var a =[],b= function () { return a.length<100?(a.push(a.length)&&arguments.callee()):a }(); console.log(a,b);

kylewh commented 7 years ago

[...Array(10).keys()]

zhishaofei3 commented 6 years ago
Array(100).fill().map((v, i) => i)
liejiayong commented 5 years ago

[...Array(100).keys()]