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?

0

1 Answer 1

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]);

Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.