Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

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

15 Answers 15

up vote 21 down vote accepted

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

share|improve this answer
3  
Beautiful. Absolutely beautiful. –  Briguy37 Jan 25 '13 at 16:27
1  
Damn, I ended up with: c=[b=[i=10]];while(i--)b[i]=c,c[i]=1. Should have known!!! –  mowwwalker Jan 27 '13 at 22:35

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]
share|improve this answer
    
Very clever update! –  Shmiddty Jan 25 '13 at 4:40
    
for(a=[i=9];i;)a[i--]=[1,1,1,1,1,1,1,1,1,1] –  AmericanUmlaut Jan 25 '13 at 10:25
    
+1, two very nice solutions. –  grc Jan 25 '13 at 14:05
1  
@AmericanUmlaut that doesn't work –  copy Jan 25 '13 at 16:04
    
I like ^ for != –  Jan Dvorak Jan 25 '13 at 17:21

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!

share|improve this answer
3  
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 –  w0lf Jan 25 '13 at 13:21
    
a very nice solution nevertheless (+1) –  w0lf Jan 25 '13 at 15:46

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))
share|improve this answer
    
Nice abuse of the ... operator! –  nderscore May 23 at 19:34

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)
share|improve this answer
    
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 Jan 25 '13 at 1:13
    
Yeah I didn't see that. I've updated my answer. –  grc Jan 25 '13 at 2:38

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;);
share|improve this answer
    
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 Jan 25 '13 at 14:49
    
@mellamokb, thanks for the tip :-) –  FakeRainBrigand Jan 25 '13 at 15:16

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)
share|improve this answer
1  
You can save one char by initializing i in a like this: a=[i=10] –  air_blob Jan 31 '13 at 9:44

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.

share|improve this answer
    
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 Jan 27 '13 at 22:39
1  
a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]") has 56 –  Anonymous Mar 12 '13 at 21:58

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

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
share|improve this answer

39 bytes

Using array comprehension in ECMAScript 6:

x=[x for(y in x=[1,1,1,1,1,1,1,1,1,1])]
share|improve this answer

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.

share|improve this answer
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. –  Jan Dvorak Jan 25 '13 at 18:06

Javascript 69 characters (for now)

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

JavaScript 73

function h(){s="1111111111",r=[10],i=-1;while(++i<10){r[i]=s.split("");}}
share|improve this answer
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 May 25 at 7:18

42

Update: edited total chars to reflect function. Also, for those of you who are telling me that I have not implemented an array as requested, well, it is a different way of looking at the problem. I'll stand by my answer :)

If the array values will never change (and only be accessed via get queries, then it is always easier to make a function out of it:

function a(i,j){return (i<10&&j<10)?1:0}

The body of the function is 18 chars - whatever is inside the {...} after the return. Note that this does not cover negative values, which the array object requested wouldn't tolerate anyway.

share|improve this answer
    
-1 (if I had rep on this SE) - Your code is 64 bytes and does not create a 10x10 array of 1s. –  AmericanUmlaut Jan 25 '13 at 10:24
    
Welcome to SE Code Golf, could you please refactor your answer to meet the questions requirements and add the correct character count? –  air_blob Jan 25 '13 at 14:22
1  
Hi! Every character that is necessary to implementation must be counted. (In fact, in some languages, we even count flags passed into a command-line interpreter as additional characters.) In this case, your score is currently 56 (leaving out unnecessary ; and whitespace), but as @AmericanUmlaut says, this code doesn't follow the spec, because it doesn't create an actual instance of a 10x10 array of 1s. –  mellamokb Jan 25 '13 at 14:54
    
Thanks for pointing the errors out, and special shout-out to @air_blob for the welcome :) I just looked at the problem differently - you can down-vote if you want –  Sudipta Chatterjee Jan 25 '13 at 16:39
    
@SudiptaChatterjee The point of a code golf is to make the shortest possible code that performs some given task. Your code doesn't perform the task (-1), and it's not the shortest possible either (there is some useless whitespace). –  Jan Dvorak Jan 25 '13 at 17:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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