简体   繁体   English

将数组中的元素随机存储到表中

[英]Randomly storing elements from an array into a table

I'm trying to create a simple memory matching game and I'm having trouble assigning one number to each table cell from my cardValues array. 我正在尝试创建一个简单的内存匹配游戏,但是我在从cardValues数组中为每个表单元分配一个数字时遇到了麻烦。 My giveCellValue function is supposed to generate a random number then pick that number from the array and give it to one of the table cells but I'm a little over my head on this one and having trouble accomplishing this task. 我的GiveCellValue函数应该生成一个随机数,然后从数组中选择该数字并将其提供给表单元格之一,但是我对此有点不知所措,并且很难完成此任务。

var countCells;
var cardValues = [];
var checker = true;
var createTable = function (col, row) {
    $('table').empty();
    for (i = 1; i <= col; i++) {
        $('table').append($('<tr>'));
    }
    for (j = 1; j <= row; j++) {
        $('tr').append($('<td>'));
    }
    countCells = row * col;
};
createTable(3, 6);
for (i = 1; i <= countCells / 2; i++) {
    cardValues.push(i);
    if (i === countCells / 2 && checker) {
        checker = false;
        i = 0;
    }
}

var giveCellValue = function () {
    var random = Math.ceil(Math.random() * cardValues.length) - 1;
    for (i = 0; i <= cardValues.length; i++) {
        $('td').append(cardValues[random]);
        cardValues.splice(random, 1);
    }
};

giveCellValue();
console.log(cardValues);

Use 采用

var countCells;
var cardValues = [];
var checker = true;

var createTable = function (col, row) {
    $('table').empty();
    for (var i = 0; i < col; i++) {
        $('table').append($('<tr>'));
    }
    for (var j = 0; j < row; j++) {
        $('tr').append($('<td>'));
    }
    countCells = row * col;
};
createTable(3, 6);

for (i = 0; i < countCells; i++) {
    cardValues.push(i % 9 + 1);
}

var giveCellValue = function () {
    var len = cardValues.length, tds = $('td');
    for (var i = 0; i < len; i++) {
        var random = Math.floor(Math.random() * cardValues.length);
        tds.eq(i).append(cardValues.splice(random, 1));
    }
};

giveCellValue();
console.log(cardValues);

Demo: Fiddle 演示: 小提琴

Leaving aside the problems that have already been mentioned in the comments above... 撇开上面评论中已经提到的问题...

If I understand it right, you want to assign each of the numbers in cardValues to a random table cell? 如果我理解正确,您想将cardValues中的每个数字分配给随机表单元格吗?

Seems like the problem is with this line: 似乎问题在于此行:

var random = Math.ceil(Math.random() * cardValues.length) - 1;

What this does is generates a random number once . 这是一次生成一个随机数。 If you access the variable random at a later time, you're not calling the full line of code again, you're just getting the same value that was calculated the first time. 如果以后再访问变量random ,就不会再调用整个代码行,而只会获得与第一次计算时相同的值。 Eg if the above code runs, spits out '7' as its random number and stores that in random , then every time you go through the for loop, the value of random will always be '7' rather than being re-generated every time - make sense? 例如,如果上面的代码运行,吐出来的“7”作为它的随机数并存储在random ,那么每一次你去通过for循环的价值random永远是“7”,而不是重新生成每次- 说得通?

Try putting the randomiser inside the for loop - that way it will be run multiple times, generating a new random number every time: 尝试将randomizer 放入 for循环中-这样它将多次运行,每次都生成一个新的随机数:

var giveCellValue = function () {
    var random;
    for (i = 0; i <= cardValues.length; i++) {
        random = Math.ceil(Math.random() * cardValues.length) - 1;
        $('td').append(cardValues[random]);
        cardValues.splice(random, 1);
    }
};

Actually, I'd change line 4 above to random = Math.floor(Math.random() * cardValues.length); 实际上,我将上面的第4行更改为random = Math.floor(Math.random() * cardValues.length); too which should have the same effect. 也应该具有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM