I want to have an object made of arrays containing integers as indicators used by another method. Each array of integers have to be different, but my function has to randomly create them and while adding a new array make sure that it doesn't exist already, but at the same time a specific amount of arrays have to be added.
Lets say I want an object that look like this:
arr = {
0: [0,0],
1: [0,1],
2: [1,1],
3: [1,0]
}
The above object can be in different order of course, depending on the random aspect of below Math.random
.
That's the code I have:
let arrayOfPositions = [];
let i = 0;
let position = [];
do {
let randX = Math.round(Math.random());
let randY = Math.round(Math.random());
position = [randX, randY];
arrayOfPositions[i] = position;
i++;
}
while (arrayOfPositions.length < 4 );
There's something missing in my while condtition to check if the position
already exist in the arrayOfPositions
. I tried indexOf
and lastIndexOf
but it then always stopped after first array added.