显然,使用单独的函数和库将面临艰巨的挑战,因此不允许使用它们。
您的代码必须符合ECMAscript规范(任何规范都可以),因此没有针对浏览器的答案。
实例化后,该数组必须可访问。
我有一个暂时不回答的答案。
注意:这项挑战是特定于javascript的,因为众所周知,使用javascript制作多维数组很不方便。
[10]
[[10]]
吗?
显然,使用单独的函数和库将面临艰巨的挑战,因此不允许使用它们。
您的代码必须符合ECMAscript规范(任何规范都可以),因此没有针对浏览器的答案。
实例化后,该数组必须可访问。
我有一个暂时不回答的答案。
注意:这项挑战是特定于javascript的,因为众所周知,使用javascript制作多维数组很不方便。
[10]
[[10]]
吗?
Answers:
(x=i=>Array(10).fill(i))(x(1))
ES6运行中:)
for(r=[b=[i=10]];i--;r[i]=b)b[i]=1
由于通过引用使行相等显然是可以的,因此我认为依靠这一事实显然可以。通过与表的行同时构建表,这有助于我们消除一个for循环。所以,这是我的新选手。
由于r[0]
和b[0]
在循环期间被覆盖,因此它们可能包含垃圾。这为我们提供了另一个免费执行插槽,可省去一些逗号。可悲的是,r=b=[]
不会这样做,因为从那时起,它们就等于副裁判。
而且,它可以很好地扩展(99x99仍然是34个字节),不需要ES5(无论如何,新功能的名称都非常长),并且,此外,它不可读:-)
c=[b=[i=10]];while(i--)b[i]=c,c[i]=1
。应该知道!!!
x=(y=[..."1111111111"]).map(x=>y)
输出“ 1”的10x10数组。
这滥用了以下事实:字符串“ 1111111111”具有所有必需的属性,就好像它是一个数组一样,因此您可以使用spread运算符...
将其转换为字符数组,然后将其映射为该数组的副本每个引用“原始”元素的元素。
或仅使用一个变量名(35个字符):
x=(x=x=>[..."1111111111"])().map(x)
或造成其他混乱(但以45个字符为单位)
x=[];x[9]=x;x=[...x].map(y=>[...x].map(x=>1))
或(43个字符)
y=y=>[...x].map(x=>y);x=[];x[9]=x;x=y(y(1))
...
操作员的滥用!
x=[a=[1,1,1,1,1,1,1,1,1,1],a,a,a,a,a,a,a,a,a]
每个元素都指向相同的数组,但是规范没有提及任何反对!
x=
开始就应该多加一些
a=[];for(i=10;i;i--,a.push([1,1,1,1,1,1,1,1,1,1]));
Or if all indices are allowed to point to the same array:
a=[1,1,1,1,1,1,1,1,1,1];a=a.map(a.sort,a)
a[0][1] = 5; console.log(a[1][1])
).
Best I have so far. (Still trying).
x=[];while(x.push([1,1,1,1,1,1,1,1,1,1])-10);
Shorter (thanks mellamokb!)
for(x=[];x.push([1,1,1,1,1,1,1,1,1,1])-10;);
for
is almost always preferable to while
when golfing. The basic statement is the same length (while()
vs for(;;)
), but for
gives you more flexibility. In this case, you could put x=[]
in the initializer of the for
and save one character.
Since my original solution has been beaten, I will now post it.
for(a=[],i=10;i--;a[i]='1111111111'.split(''));
Unfortunately, 0x3FF.toString(2)
isn't quite as efficient as just listing the string out, which isn't quite as efficient as just statically declaring the array.
You can shave off one character this way (46):
for(a=[],i=10;i--;a[i]=[1,1,1,1,1,1,1,1,1,1]);
You can save 2 more characters like so: (44)
for(a=[i=10];i--;)a[i]=[1,1,1,1,1,1,1,1,1,1]
Another 44 byte solution using JS 1.8 function expression closures (FF only atm):
x=(x=[1,1,1,1,1,1,1,1,1,1]).map(function()x)
i
in a
like this: a=[i=10]
r=eval("["+(A=Array(11)).join("["+A.join("1,")+"],")+"]")
Before golfing:
a=Array(11).join("1,");
b=Array(11).join("["+a+"],")
c=eval("["+b+"]")
Note: This needs ES5, so don't expect much from IE.
eval('a=['+(b=Array(11)).join('['+b.join("1,")+'],')+']')
. Apart from having different quotes and my having the variable inside eval, these are exactly the same
a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]")
has 56
This has already been beaten, but here's my solution:
function x(){return[1,1,1,1,1,1,1,1,1,1]}x=x().map(x)
x=
before x().map
(or y=
, etc) to make it accessible, right?
Using array comprehension in ECMAScript 7:
x=[x for(y in x=[1,1,1,1,1,1,1,1,1,1])]
[1,1,1,1,1,1,1,1,1,1]
with Array(10).fill(1)
shaves 4 chars.
(a=[b=[]]).fill(b.fill(a[9]=b[9]=1))
Tested in my browser (Firefox on Ubuntu) and in the Codecademy Javascript interpreter
It's not the shortest, but I thought I'd go for a different approach to what's already here.
a=(''+{}).split('').slice(0,10).map(_=>a.map(_=>1))
Cast an object to a string
"[object Object]"
Split the string into chars ["[", "o", "b", ... "]" ]
Grab a slice of just 10
Map the result of mapping 1 onto the initial slice
a
already existed that version seemed to have worked. Updated version should work in FF and node with the --harmony flags.
for(var a=b=[],i=100;i-->0;b[i%10]=1)if(i%10==0){a[i/10]=b;b=[]}
for(var a=b=[i=100];i-->0;b[i%10]=1)if(i%10==0){a[i/10]=b;b=[]}
One char saved by hiding the iterator in the array.
for(a=b=[1],i=100;i--;b[i%10]=1)if(i%10==0){a[i/10]=b;b=[]}
Saved some chars with Jans suggestions.
Readable version:
for(var a = b = [i = 100]; i-- > 0; b[i % 10] = 1) {
if(i % 10 == 0) {
a[i / 10] = b;
b = [];
}
}
Yes, it's not the shortest solution, but another approach without using readymade [1,1,1,1,1,1,1,1,1,1]
arrays.
i-->0
to i--
. While the "downto" operator is nice, it's not really neccessary. ==0
could be shaved off by two bits as well. Also, leave out var
. While normally useful, four useless bytes are just unacceptable here.
Javascript 69 characters (for now)
b=[]
a=[]
for(i=100;i-->0;){
a.push(1)
if(i%10===0){b.push(a);a=[]}
}
function h(){s="1111111111",r=[10],i=-1;while(++i<10){r[i]=s.split("");}}
r=[10],
does nothing as you immediately overwrite r[0]
. You don't need to declare s
and could just do "1111111111".split("")
but that is 22 characters whereas [1,1,1,1,1,1,1,1,1,1]
is only 21 characters.