javascript - 为什么这是无限循环?

标签 javascript arrays loops

我试图将三个字符串放入一个大小为 9 的数组中,该数组将三个字符串放置在数组中的随机索引处,但我试图这样做会导致无限循环

const ticketArray = [];
const victoryMultipliers = ["x10", "x20", "x30", "x40", "x50", "x60"];

const threeOfThem = [];
let arr = []
let randStr = "";
for (let i = 0; i < 3; i++) {
    threeOfThem[i] = victoryMultipliers[Math.floor(Math.random() * victoryMultipliers.length)]
    randStr = threeOfThem[i]
    //console.log(randStr)

    arr = threeOfThem.filter(val => val === randStr)

    if (arr.length >= 2) {
        threeOfThem.pop();
        i--;
    }

}
const threeTicketArray = threeOfThem;
/// So above we have an array of example ["x20", "x50", "x30"]

// so below is where we have some problem

let randInt;
let tempStr = "";
for (let i = 0; i < 10; i++) { // i want to make an array.length of 9
    randInt = Math.floor( Math.random() * 3 );
    tempStr = threeTicketArray[randInt]; // to find the "x30" or whichever i stored

  ticketArray.push(tempStr); // push it into the ticketArray
  let len = ticketArray.filter(val => val === tempStr) // we filter to bring out the once for this loops iteration

    if ( len.length > 3 ) { // if I have 4 of example "x30"
        ticketArray.pop(); // I remove it as it was the last "x30" string added to the array
      i--; // I subtract the iteration by 1 so I can restart the this iteration of the loop
    }
}
// the end of the loop
console.log(ticketArray);

因此,我随机希望 ticketArray 的大小为 9,将票证字符串放置在随机索引处,并且每个字符串出现 3 次。但是,我遇到了无限循环,我不明白为什么!

最佳答案

请参阅下面的解决方案,并告诉我您的想法

const ticketArray = [];
const victoryMultipliers = ["x10", "x20", "x30", "x40", "x50", "x60"];

const threeOfThem = [];
let arr = []
let randStr = "";
for (let i = 0; i < 3; i++) {
    threeOfThem[i] = victoryMultipliers[Math.floor(Math.random() * victoryMultipliers.length)]
    randStr = threeOfThem[i]
    //console.log(randStr)

    arr = threeOfThem.filter(val => val === randStr)

    if (arr.length >= 2) {
        threeOfThem.pop();
        i--;
    }
    
}
const threeTicketArray = threeOfThem;
/// So above we have an array of example ["x20", "x50", "x30"]

// so below is where we have some problem


let randInt;
let tempStr = "";
for (let i = 0; i < 10; i++) { // i want to make an array.length of 9
    randInt = Math.floor( Math.random() * 3 );
    tempStr = threeTicketArray[randInt]; // to find the "x30" or whichever i stored

  ticketArray.push(tempStr); // push it into the ticketArray
  let len = ticketArray.filter(val => val === tempStr) // we filter to bring out the once for this loops iteration
  
    if ( len.length > 3 ) { // if I have 4 of example "x30"
        ticketArray.pop(); 
        // this should solve the infinit loop issue. 
        // as it seems that randInt is generating duplicated.
        if (ticketArray.length <9)
           i-- 
    }
}
// the end of the loop
console.log(ticketArray);

关于javascript - 为什么这是无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72623790/

相关文章:

javascript - Angular 2 生命周期钩子(Hook)未调用(ES5 javascript)

javascript - 如何使用 jquery 使我的 div 在两侧 400 px 内生成?

javascript - 如何根据 ng-if 打破 <tr ng-repeat> 显示单行?

java - 打印不同长度数组的嵌套循环

javascript - 模态框无法正常工作

python - 关于 python 中列表交集的快速问题

javascript - 使用 underscore.js 对多个属性进行对象数组过滤

javascript - jQuery 用格式写入每个字符

java - 基于用户输入的增量值循环

loops - 了解循环不变式。寻找并证明它们的算法