Javascript:创建一个1x的10x10数组


18

显然,使用单独的函数和库将面临艰巨的挑战,因此不允许使用它们。

您的代码必须符合ECMAscript规范(任何规范都可以),因此没有针对浏览器的答案。

实例化后,该数组必须可访问。

我有一个暂时不回答的答案。

注意:这项挑战是特定于javascript的,因为众所周知,使用javascript制作多维数组很不方便。


我有57个字符。这是一个好分数吗?
John Dvorak

好吧,绝对不是:-)
约翰·德沃夏克

15
您确定不只是想要这个10s的漂亮的1x1数组吗?[10]
kojiro

8
那会是[[10]]吗?
yingted 2013年

Answers:


10

Javascript ES6、30

(x=i=>Array(10).fill(i))(x(1))

ES6运行中:)


2
嗨,这个问题大约2岁了。这可能是更好地利用JS代码打高尔夫球的技巧来回答更多最近的问题的;)
乔治

6
@乔治我认为回答旧挑战没有任何问题。(无论挑战年龄有多大,如果有新的获胜者,一些挑战者的作者也会更新接受的答案。)
Martin Ender 2015年

2
@MartinBüttner这个答案难以接受,因为ES6比这个挑战还年轻(它于2015年推出:)),而Array.fill()仅是ES6 :)
Katenkyo

我刚刚回答了,因为我也看到了其他ES6回答。我认为这没有任何问题。作者可能会更新他的问题,而其他人可能会采用多种方法来解决上述问题。
阿方索·马托斯

ES6在其草案真正冻结之前很久就被接受(也就是在几天前的2015年)。我不确定这是正确还是错误。
Optimizer

27

Javascript,34个字节

for(r=[b=[i=10]];i--;r[i]=b)b[i]=1

由于通过引用使行相等显然是可以的,因此我认为依靠这一事实显然可以。通过与表的行同时构建表,这有助于我们消除一个for循环。所以,这是我的新选手。

由于r[0]b[0]在循环期间被覆盖,因此它们可能包含垃圾。这为我们提供了另一个免费执行插槽,可省去一些逗号。可悲的是,r=b=[]不会这样做,因为从那时起,它们就等于副裁判。

而且,它可以很好地扩展(99x99仍然是34个字节),不需要ES5(无论如何,新功能的名称都非常长),并且,此外,它不可读:-)


3
美丽。简直美极了。
Briguy37

1
该死,我最后得到了:c=[b=[i=10]];while(i--)b[i]=c,c[i]=1。应该知道!!!
mowwwalker 2013年

19

ECMAScript 6-33个字符

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))

...操作员的滥用!
nderscore 2014年

13

44个字节

for(a=[i=10];i;)a[--i]=[1,1,1,1,1,1,1,1,1,1]

先前版本:

for(a=i=[];i^10;)a[i++]=[1,1,1,1,1,1,1,1,1,1]

非常聪明的更新!
2013年

for(a = [i = 9]; i;)a [i-] = [1,1,1,1,1,1,1,1,1,1,1]
AmericanUmlaut

1
@AmericanUmlaut无法正常工作
复制

1
我喜欢^!=
约翰·德沃夏克

13

45个字符

x=[a=[1,1,1,1,1,1,1,1,1,1],a,a,a,a,a,a,a,a,a]

每个元素都指向相同的数组,但是规范没有提及任何反对!


4
规范说:“数组在实例化后必须可以访问。” 。我想这意味着您一x=开始就应该多加一些
Cristian Lupascu 2013年

不过,这是一个非常不错的解决方案(+1)
Cristian Lupascu 2013年

That is not very practical though as a change to a value will reflect on all arrays.
Gabriele Petrioli

7

Javascript, 51

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:

Javascript, 41

a=[1,1,1,1,1,1,1,1,1,1];a=a.map(a.sort,a)

All of the indices now point to the same column, which is probably unwanted. (Try a[0][1] = 5; console.log(a[1][1])).
copy

Yeah I didn't see that. I've updated my answer.
grc

6

JavaScript, 45 44 Bytes

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;);

Note that 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.
mellamokb

@mellamokb, thanks for the tip :-)
Brigand

5

Javascript, 47

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)

1
You can save one char by initializing i in a like this: a=[i=10]
codeporn

4

JavaScript, 57 bytes

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.


Wow! I wrote 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
mowwwalker

2
a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]") has 56
yingted

3

54

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)

Arrow functions will do interesting things to JavaScript golfing. Now if only I could figure out how to trivially Curry builtins.
kojiro

You'd need to insert x= before x().map (or y=, etc) to make it accessible, right?
Shmiddty

@Schmiddty Yes, so updated.
kojiro

3

39 bytes

Using array comprehension in ECMAScript 7:

x=[x for(y in x=[1,1,1,1,1,1,1,1,1,1])]

There are no array comprehensions in ES6, that's an ES7 proposal.
Afonso Matos

@afonsomatos at the time I made this answer, it was still part of the ES6 draft. It's updated now though :)
nderscore

Replacing [1,1,1,1,1,1,1,1,1,1] with Array(10).fill(1) shaves 4 chars.
Shmiddty

2

56 characters

Rather long answer, but will be better for 100x100 etc.

for(a=[[i=k=0]];++i<100;m?0:a[k=i/10]=[1])a[k][m=i%10]=1

2

Javascript, 36 chars (ECMA 6)

(a=[b=[]]).fill(b.fill(a[9]=b[9]=1))

Tested in my browser (Firefox on Ubuntu) and in the Codecademy Javascript interpreter


2

ES6, 54

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


Where can I test it? It doesn't work right in my FF.
Qwertiy

I'd missed out some parens, however, because a already existed that version seemed to have worked. Updated version should work in FF and node with the --harmony flags.
Dan Prince

Now it returns right array, but it's not assigned to any variable.
Qwertiy

1

61 for fun

eval("r=["+("["+(Array(11).join("1,"))+"],").repeat(11)+"]")

0

Javascript - 64 63 59 characters

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.


1
Change 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.
John Dvorak

0

Javascript 69 characters (for now)

b=[]
a=[]
for(i=100;i-->0;){
a.push(1)
if(i%10===0){b.push(a);a=[]}
}

0

JavaScript 73

function h(){s="1111111111",r=[10],i=-1;while(++i<10){r[i]=s.split("");}}

1
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.
MT0

0

Javascript ES6, 53

x="1".repeat(100).match(/.{10}/g).map(s=>s.split(""))

But the ones in the array are strings :)


0

Javscript ES6, 53

x=eval("["+("["+"1,".repeat(10)+"],").repeat(10)+"]")

0

JavaScript (ES6), 35 bytes

A=Array(10).fill(Array(10).fill(1))

1
Setting a variable isn't an approved method of output. However, you can do f=>Array(10).fill(Array(10).fill(1)) (defining an anonymous function) for 36 bytes.
Rɪᴋᴇʀ
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.