关于寻找数字中的无序整数的小dom

66 阅读1分钟
  1. 首先这个类的第一参数是你输入的最大值,会在这个最大值里面去筛选随机数
  2. 第二个参数是你所排除的数字
  3. 第三个参数是你需要输出的个数
class NumberFinder {
  constructor(maxValue, excludedNumbers, outputCount) {
    this.maxValue = maxValue;
    this.excludedNumbers = excludedNumbers;
    this.outputCount = outputCount;
  }

  findNumbers() {
    let numbers = [];
    let i = 1;
    while (numbers.length < this.outputCount && i <= this.maxValue) {
      if (!this.excludedNumbers.includes(i)) {
        numbers.push(i);
      }
      i++;
    }
    let result = [];
    while (result.length < this.outputCount) {
      let randomNum = Math.floor(Math.random() * this.maxValue) + 1;
      if (!this.excludedNumbers.includes(randomNum) && !result.includes(randomNum)) {
        result.push(randomNum);
      }
    }
    return result;
  }
}

const numberFinder = new NumberFinder(10, [3], 4);
console.log(numberFinder.findNumbers()); // [1, 2, 4, 5]