Javascript |产生号码

时间:2019-02-26 19:26:34

标签: javascript

我要编写一个JavaScript代码,该代码会生成一个数组allColumns,其中包含特定数量的其他数组,

内部数组包含随机选择的特定数量的INT;

问题

此代码生成重复的数组,我不知道为什么!有帮助吗?

function randomNumberFromRange(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}


get_random = function (list) {
    return list[Math.floor((Math.random() * list.length))];
}



function getSum(total, num) {
    return total + num;
}
let columnLenght = 3;
    let i = 0;
    let j = 0;
    let tableColumns = 3;
    let allColumns = [];
    let prevNumber = [];

    let prevResult = 0;

    while (j < tableColumns) {
        while (i < columnLenght) {

            if (prevNumber === undefined || prevNumber.length == 0) {
                let randomNumber = get_random([1, 2, 3, 4], i);
                $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                prevNumber.push(randomNumber);

            } else {

                prevResult = prevNumber.reduce(getSum);
                if (prevResult == 0) {
                    let randomNumber = get_random([0, 1, 2, 3, 4]);
                    $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                    prevNumber.push(randomNumber);

                }
                if (prevResult == 1) {
                    let randomNumber = get_random([-1, 0, 1, 2, 3]);
                    $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                    prevNumber.push(randomNumber);

                }
                if (prevResult == 2) {
                    let randomNumber = get_random([-2, -1, 0, 1, 2]);
                    $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                    prevNumber.push(randomNumber);

                }
                if (prevResult == 3) {
                    let randomNumber = get_random([-3, -2, -1, 0, 1]);
                    $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                    prevNumber.push(randomNumber);

                }
                if (prevResult == 4) {
                    let randomNumber = get_random([-4, -3, -2, -1, 0]);
                    $('.testcontent').append('<h6 class="row">' + randomNumber + '</h6>');
                    prevNumber.push(randomNumber);

                }
            }
            i++;

        }
        console.log(prevNumber);
        allColumns.push(prevNumber);
        j++;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:0)

因为您没有在每个新行上重置阵列。您只需要添加到相同的数组,然后将其追加到另一个数组即可。因此,当您开始一行循环时,请创建一个新数组以推入。

while (j < tableColumns) {
  let prevNumber = []; // redefine your array on each row
  let i = 0  // reset i
  while (i < columnLength) { // <-- spelling error
    /* code */
  }
  allColumns.push(prevNumber);
  j++;
}

不确定为什么不使用for循环

for (let j=0; j < tableColumns; j++) {
  const prevNumber = []
  for (let i=0; i < columnLength; i++) {
    /* code */
  }
  allColumns.push(prevNumber);
}