Tersest在JavaScript中从1..20创建一个整数数组的方式 [英] Tersest way to create an array of integers from 1..20 in JavaScript

查看:124
本文介绍了Tersest在JavaScript中从1..20创建一个整数数组的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是创建这个数组的tersest方式:

What would be the tersest way to create this array:

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
         11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

例如,一个循环:

var x = [];
for (var i=1;i<=20;i++) {
  x.push(i);
}

,而循环:

var x = [], i = 1, endInt = 20;
while (i <= endInt) {
  x.push(i);
  i++;
}

会不会有其他的例子,这将是更简洁 - 换句话说 - 少code?我想的东西像红宝石,其中相当于code,我相信会是那样简单 1..20 。我不知道这样的JavaScript语法,但我不知道是否有更短的方式做同样的事情。

Would there be other examples that would be terser -- in other words -- less code? I'm thinking of things like in Ruby where the equivalent code I believe would be as simple as 1..20. I'm not aware of syntax like that in JavaScript but I'm wondering if there are shorter ways to do that same thing.

更新:我并没有考虑删除分号或 VAR 寻找答案的问题,但我不得不承认这个问题意味着, 。我比较好奇的算法比剃须字节。很抱歉,如果我不清楚!此外,使它成为一个功能非常简单,只需一巴掌的功能范围(起始,结束){/ *胆量在这里* /} 周围和你的存在。现在的问题是是否有新的方法,以胆。

UPDATE: I wasn't thinking of removing semicolons or var for answers in the question, but I have to admit the question implies that. I am more curious about algorithms than shaving bytes. Sorry if I was unclear! Also, making it into a function is simple enough, just slap function range(start, end) { /* guts here */ } around it and you're there. The question is are there novel approaches to the "guts."

推荐答案

想有点后,这是最短的执行标准范围(N)功能在JavaScript我能想出:

After thinking about it a bit, this is the shortest implementation of the standard range(N) function in JavaScript I could come up with:

function range1(i){return i?range1(i-1).concat(i):[]}

注意:不要在生产中使用这一点;它的O(N ^ 2)

Note: Do not use this in production; it's O(N^2)

对比与目前顶级投回答:

Contrast with current top-voted answer:

function range1(i){var x=[];var i=1;while(x.push(i++)<i){};return x}


例如:


Example:

> range1(5)
[1, 2, 3, 4, 5]


这是像典范递归,但我希望它会更长,直到我认为三元if语句,它把它降低到42需要的字符。


This is like the poster child for recursion, though I was expecting it to be longer until I thought of ternary-if-statement, which brings it down to 42 necessary characters.

请注意,标准范围函数返回[开始,结束)可以写做 .concat(I-1)

Note that the "standard" range function returning [start,end) can be written by doing .concat(i-1).

更新:哦,我被滥用的循环发现了一个令人难以置信的短版丑陋强制性语法,反向排序,事实上,分配返回值:的(Y = [],I = 20 ; Y [ - 我] =我;){} 仅由25个字符(虽然你会希望 VAR是,你可以插入一个for循环,和+1如果你不想0 ... 19)。虽然不短,如果你需要定义一个函数,它比短 I R(I-1).concat(I):[] 如果不这样做需要进行功能

Update: Ooh, I discovered an incredibly short version with ugly imperative syntax by abusing for loops, reverse-ordering, the fact that assignments return a value: for(y=[],i=20;y[--i]=i;){} consisting of only 25 characters (though you will want var y which you can insert into a for loop, and +1 if you don't want 0...19). While it is not shorter if you need to define a function, it is shorter than i?r(i-1).concat(i):[] if you do not need to make a function.

更新Sep13,2015:

Update Sep13,2015:

刚刚想出了这个它与支持ES6标准的浏览器的工作原理新方法:

Just came up with this new method which works with browsers which support the ES6 standard:

> Array(5).fill().map((x,i)=>i)
[0, 1, 2, 3, 4]

这篇关于Tersest在JavaScript中从1..20创建一个整数数组的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆