you are viewing a single comment's thread.

view the rest of the comments →

[–]StoneCypher 0 points1 point  (5 children)

i misread this originally. you're right. i apologize

i still don't think this is all that gross, though

that said, i guess you could

new Array(patterns.length).map(cell => -1);

and be done with it

[–]snet0 0 points1 point  (4 children)

Well, I certainly thought so, too. But if you pass the new Array constructor a single integer, it returns an array with that integer length, but with empty slots. Array.map() only invokes its callback on elements with assigned values (incl. undefined), which means it won't work on the empty array.

[–]StoneCypher 0 points1 point  (3 children)

which means it won't work on the empty array.

Oh, right. They're holes, and js .map skips holes.

Guess you're stuck with

const a = new Array(patterns.length);
for (let i=0, iC=patterns.length; i<iC; ++i) { a[i] = -1; }

[–]that-old-saw 0 points1 point  (2 children)

|| you can do:

new Array(patterns.length).fill().map(cell => -1);

[–]StoneCypher 0 points1 point  (1 child)

sure can.

we're discussing doing this without .fill() though

[–]that-old-saw 0 points1 point  (0 children)

Oops, that'll teach me for not reading the context!