Looking to obtain an array of arrays with unique values, but running into issues when
while
loop seems to get skipped or overlooked (possibly due to the asynchronous nature of it). Would appreciate assistance in implementing a promise within this code snippet, or utilizing
async/await
methods, or receiving better guidance on how to properly handle these arrays. Attempted adding
async/await
functionality but encountered errors and unsure about where to integrate a promise effectively. The current function is as follows:
getSeveralArrays() {
for (let index = 0; index < 50; index++) {
this.getValue();
}
}
getValue() {
this.i++;
this.array = [];
this.randomArray = [];
for (let index = 0; index < 4; index++) {
this.randomValue = this.getRandom();
if (this.array.length === 2) {
while ((this.array[0] === this.randomValue) || (this.array[1] === this.randomValue)) {
this.randomValue = this.getRandom();
}
this.array.push(this.randomValue);
} else if (this.array.length === 3) {
while ((this.array[0] === this.randomValue) || (this.array[1] === this.randomValue) || (this.array[2] === this.randomValue)) {
this.randomValue = this.getRandom();
}
this.array.push(this.randomValue);
} else {
this.array.push(this.randomValue);
}
console.log({...this.array});
this.randomArray.push({ind: this.i, val: this.array});
}
}
getRandom() {
const value = Math.floor(Math.random() * 4);
return value;
}