# Javascript: Create a 10x10 array of 1s

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.

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

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

• Beautiful. Absolutely beautiful. Jan 25, 2013 at 16:27
• Damn, I ended up with: c=[b=[i=10]];while(i--)b[i]=c,c[i]=1. Should have known!!! Jan 27, 2013 at 22:35

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

• Nice abuse of the ... operator! May 23, 2014 at 19:34

# 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]

• Very clever update! Jan 25, 2013 at 4:40
• for(a=[i=9];i;)a[i--]=[1,1,1,1,1,1,1,1,1,1] Jan 25, 2013 at 10:25
• @AmericanUmlaut that doesn't work
– copy
Jan 25, 2013 at 16:04
• I like ^ for != Jan 25, 2013 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!

• 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 Jan 25, 2013 at 13:21
• a very nice solution nevertheless (+1) Jan 25, 2013 at 15:46
• That is not very practical though as a change to a value will reflect on all arrays. Jan 10, 2019 at 14:33

# Javascript ES6, 30

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


ES6 in action :)

• 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 ;) Jun 18, 2015 at 22:47
• @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.) Jun 19, 2015 at 7:18
• @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 :) Jun 19, 2015 at 8:24
• 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. Jun 19, 2015 at 10:33
• 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. Jun 19, 2015 at 11:10

## 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
Jan 25, 2013 at 1:13
• Yeah I didn't see that. I've updated my answer.
– grc
Jan 25, 2013 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;);

• 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. Jan 25, 2013 at 14:49
• @mellamokb, thanks for the tip :-) Jan 25, 2013 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)

• You can save one char by initializing i in a like this: a=[i=10] Jan 31, 2013 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.

• 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 Jan 27, 2013 at 22:39
• a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]") has 56 Mar 12, 2013 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)

• Arrow functions will do interesting things to JavaScript golfing. Now if only I could figure out how to trivially Curry builtins. Jan 25, 2013 at 4:56
• You'd need to insert x= before x().map (or y=, etc) to make it accessible, right? Jan 25, 2013 at 4:56
• @Schmiddty Yes, so updated. Jan 25, 2013 at 5:02

# 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. Jun 19, 2015 at 14:19
• @afonsomatos at the time I made this answer, it was still part of the ES6 draft. It's updated now though :) Jun 19, 2015 at 14:29
• Replacing [1,1,1,1,1,1,1,1,1,1] with Array(10).fill(1) shaves 4 chars. Jun 19, 2015 at 21:36

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


# 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

# 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. Feb 5, 2015 at 12:48
• 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. Feb 5, 2015 at 14:31
• Now it returns right array, but it's not assigned to any variable. Feb 5, 2015 at 15:27

# 61 for fun

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


# Javascript - 6463 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.

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.

• 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 25, 2013 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=[]}
}


## JavaScript 73

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.
– MT0
May 25, 2014 at 7:18

## Javascript ES6, 53

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


But the ones in the array are strings :)

## Javscript ES6, 53

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


# JavaScript (ES6), 35 bytes

A=Array(10).fill(Array(10).fill(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. Dec 26, 2018 at 15:42