0

I think my problm is quite simple, but I don't think I've found a way to make it work.

My goal is to : let winX = ['X', 'X', 'X']

I tried doing this with :

 let winX = [
        function () {
            for (let index = 0;index < cells_in_line;index++) {
                winX.push('X');
            }
            return winX;
        }
    ];

But a call to the console says it's undefined.

Is there a way to generate an array depending on certain variables with a function expression and a loop?

m3.b
  • 447
  • 4
  • 15

1 Answers1

0

You can use Array#fill for primitive values and Array.from for objects.

// For primitives:
let winX = Array(3).fill('X');
console.log(winX);

// For objects:
let arr = Array.from({length: 3}, _=>({x: 1}));
console.log(arr);
console.log(arr[0] !== arr[1], arr[1] !== arr[2]);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80