I cannot set while to work properly in for loops in ts

26 Views Asked by At

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;
  }
1

There are 1 best solutions below

1
phry On BEST ANSWER

There is nothing asynchronous in all your code, so no need for async/await. Also, while is 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.

class X {
  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 === 1) {
        while (this.array[0] === this.randomValue) {
          this.randomValue = this.getRandom();
        }

        this.array.push(this.randomValue);
      } else 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;
  }
}

console.log(new X().getSeveralArrays());

Also, all those checks can be simplified:

      while (this.array.some(value => value === this.randomValue)) {
        this.randomValue = this.getRandom();
      }

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.randomValue and this.array should just be defined within your function with let or const.