I want to get array of arrays with unique values, but at some point
while
gets missed or ignored (I presume because of the asynchronous nature of it). Can someone help with a adding a promise to this, or setting
async/await
structure or giving better advise how to check the arrays. I tried adding
async/await
but I get error and I am not sure where I can add a promise or use it. It is
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;
}
There is nothing asynchronous in all your code, so no need for
async/await. Also,whileis not asynchronous.You were missing a case for the length of 1 though, so the second element could always be the same as the first.
Also, all those checks can be simplified:
And generally, just as a small code review: If you do not need to access something from another class method (or somewhere outside), you should not put all those values into class properties, but keep them as values inside your functions.
So
this.randomValueandthis.arrayshould just be defined within your function withletorconst.