18
\$\begingroup\$

Obviously this challenge would be trivial with separate functions and libraries, so they aren't allowed.

Your code must conform to an ECMAscript specification (any spec will do), so no browser-specific answers.

The array must be accessible after it is instantiated.

I have an answer that I'll withhold for now.

Note: this challenge is specific to javascript because it is notoriously inconvenient to make multi-dimensional arrays in javascript.

\$\endgroup\$
  • \$\begingroup\$ I'm at 57 characters. Is this a good score? \$\endgroup\$ – John Dvorak Jan 24 '13 at 23:12
  • \$\begingroup\$ Ok, definitely not :-) \$\endgroup\$ – John Dvorak Jan 25 '13 at 10:26
  • 15
    \$\begingroup\$ Are you sure you don't just want this nice 1x1 array of 10s? [10] \$\endgroup\$ – kojiro Jan 28 '13 at 4:42
  • 8
    \$\begingroup\$ Would that be [[10]]? \$\endgroup\$ – yingted Mar 12 '13 at 21:50

21 Answers 21

10
\$\begingroup\$

Javascript ES6, 30

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

ES6 in action :)

\$\endgroup\$
  • 2
    \$\begingroup\$ Hi, this question is like 2 years old. It's probably a better use of your JS code golfing skills to answer more recent questions ;) \$\endgroup\$ – George Jun 18 '15 at 22:47
  • 6
    \$\begingroup\$ @George I don't think there's anything wrong with answering old challenges. (Some challenge authors will also update the accepted answer if a new winner comes along, no matter how old the challenge is.) \$\endgroup\$ – Martin Ender Jun 19 '15 at 7:18
  • 2
    \$\begingroup\$ @MartinBüttner This answer is not elligible for being accepted as ES6 is younger(it came in 2015 :)) than this challenge, and Array.fill() is ES6 only :) \$\endgroup\$ – Katenkyo Jun 19 '15 at 8:24
  • \$\begingroup\$ I just answered because I saw other ES6 answers too. I don't think there is anything wrong with this. The author might update his question and others might take advantage of multiple ways of solving the problem above. \$\endgroup\$ – Afonso Matos Jun 19 '15 at 10:33
  • \$\begingroup\$ ES6 has been accepted long before its draft actually got frozen (that is in 2015, a few days back). I am not sure though whether that is correct or incorrect. \$\endgroup\$ – Optimizer Jun 19 '15 at 11:10
27
\$\begingroup\$

Javascript, 34 bytes

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

Since it's apparently OK to make the rows equal by reference, I guess it's apparently OK to rely on that fact. This helps us shave off one for-loop by building the table at the same time as its rows. So, here's my new contestant.

Since r[0] and b[0] are overwritten during the loop, they can contain garbage. This gives us another free execution slot to shave off some commas. Sadly, r=b=[] won't do, since then they are equal by-ref.

Also, it scales well (99x99 is still 34 bytes), doesn't require ES5 (the new functions have terribly long names, anyways), and as a bonus, it's unreadable :-)

\$\endgroup\$
  • 3
    \$\begingroup\$ Beautiful. Absolutely beautiful. \$\endgroup\$ – Briguy37 Jan 25 '13 at 16:27
  • 1
    \$\begingroup\$ Damn, I ended up with: c=[b=[i=10]];while(i--)b[i]=c,c[i]=1. Should have known!!! \$\endgroup\$ – mowwwalker Jan 27 '13 at 22:35
19
\$\begingroup\$

ECMAScript 6 - 33 Characters

x=(y=[..."1111111111"]).map(x=>y)

Outputs a 10x10 array of "1"s.

This abuses the fact that the string "1111111111" has all the requisite properties to be treated as if it is an array so you can use the spread operator ... to transform it into an array of characters and then map it to a copy of the array with each element referencing the "original".

Or with only one variable name (for 35 characters):

x=(x=x=>[..."1111111111"])().map(x)

Or for extra confusion (but at 45 characters)

x=[];x[9]=x;x=[...x].map(y=>[...x].map(x=>1))

or (43 characters)

y=y=>[...x].map(x=>y);x=[];x[9]=x;x=y(y(1))
\$\endgroup\$
  • \$\begingroup\$ Nice abuse of the ... operator! \$\endgroup\$ – nderscore May 23 '14 at 19:34
13
\$\begingroup\$

44 bytes

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

Previous version:

for(a=i=[];i^10;)a[i++]=[1,1,1,1,1,1,1,1,1,1]
\$\endgroup\$
  • \$\begingroup\$ Very clever update! \$\endgroup\$ – Shmiddty Jan 25 '13 at 4:40
  • \$\begingroup\$ for(a=[i=9];i;)a[i--]=[1,1,1,1,1,1,1,1,1,1] \$\endgroup\$ – AmericanUmlaut Jan 25 '13 at 10:25
  • 1
    \$\begingroup\$ @AmericanUmlaut that doesn't work \$\endgroup\$ – copy Jan 25 '13 at 16:04
  • 1
    \$\begingroup\$ I like ^ for != \$\endgroup\$ – John Dvorak Jan 25 '13 at 17:21
13
\$\begingroup\$

45 characters

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

Each element points to the same array, but the spec doesn't mention anything against that!

\$\endgroup\$
  • 4
    \$\begingroup\$ the spec says that "The array must be accessible after it is instantiated.". I guess that means you should have an extra x= at the beginning \$\endgroup\$ – Cristian Lupascu Jan 25 '13 at 13:21
  • \$\begingroup\$ a very nice solution nevertheless (+1) \$\endgroup\$ – Cristian Lupascu Jan 25 '13 at 15:46
  • \$\begingroup\$ That is not very practical though as a change to a value will reflect on all arrays. \$\endgroup\$ – Gabriele Petrioli Jan 10 at 14:33
7
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ All of the indices now point to the same column, which is probably unwanted. (Try a[0][1] = 5; console.log(a[1][1])). \$\endgroup\$ – copy Jan 25 '13 at 1:13
  • \$\begingroup\$ Yeah I didn't see that. I've updated my answer. \$\endgroup\$ – grc Jan 25 '13 at 2:38
6
\$\begingroup\$

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;);
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – mellamokb Jan 25 '13 at 14:49
  • \$\begingroup\$ @mellamokb, thanks for the tip :-) \$\endgroup\$ – Brigand Jan 25 '13 at 15:16
5
\$\begingroup\$

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)
\$\endgroup\$
  • 1
    \$\begingroup\$ You can save one char by initializing i in a like this: a=[i=10] \$\endgroup\$ – codeporn Jan 31 '13 at 9:44
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – mowwwalker Jan 27 '13 at 22:39
  • 2
    \$\begingroup\$ a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]") has 56 \$\endgroup\$ – yingted Mar 12 '13 at 21:58
3
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Arrow functions will do interesting things to JavaScript golfing. Now if only I could figure out how to trivially Curry builtins. \$\endgroup\$ – kojiro Jan 25 '13 at 4:56
  • \$\begingroup\$ You'd need to insert x= before x().map (or y=, etc) to make it accessible, right? \$\endgroup\$ – Shmiddty Jan 25 '13 at 4:56
  • \$\begingroup\$ @Schmiddty Yes, so updated. \$\endgroup\$ – kojiro Jan 25 '13 at 5:02
3
\$\begingroup\$

39 bytes

Using array comprehension in ECMAScript 7:

x=[x for(y in x=[1,1,1,1,1,1,1,1,1,1])]
\$\endgroup\$
  • \$\begingroup\$ There are no array comprehensions in ES6, that's an ES7 proposal. \$\endgroup\$ – Afonso Matos Jun 19 '15 at 14:19
  • \$\begingroup\$ @afonsomatos at the time I made this answer, it was still part of the ES6 draft. It's updated now though :) \$\endgroup\$ – nderscore Jun 19 '15 at 14:29
  • \$\begingroup\$ Replacing [1,1,1,1,1,1,1,1,1,1] with Array(10).fill(1) shaves 4 chars. \$\endgroup\$ – Shmiddty Jun 19 '15 at 21:36
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Where can I test it? It doesn't work right in my FF. \$\endgroup\$ – Qwertiy Feb 5 '15 at 12:48
  • \$\begingroup\$ 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. \$\endgroup\$ – Dan Prince Feb 5 '15 at 14:31
  • \$\begingroup\$ Now it returns right array, but it's not assigned to any variable. \$\endgroup\$ – Qwertiy Feb 5 '15 at 15:27
1
\$\begingroup\$

61 for fun

eval("r=["+("["+(Array(11).join("1,"))+"],").repeat(11)+"]")
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – John Dvorak Jan 25 '13 at 18:06
0
\$\begingroup\$

Javascript 69 characters (for now)

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

JavaScript 73

function h(){s="1111111111",r=[10],i=-1;while(++i<10){r[i]=s.split("");}}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – MT0 May 25 '14 at 7:18
0
\$\begingroup\$

Javascript ES6, 53

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

But the ones in the array are strings :)

\$\endgroup\$
0
\$\begingroup\$

Javscript ES6, 53

x=eval("["+("["+"1,".repeat(10)+"],").repeat(10)+"]")
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 35 bytes

A=Array(10).fill(Array(10).fill(1))
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Rɪᴋᴇʀ Dec 26 '18 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.