如何随机播放多个数组

时间:2018-12-16 23:45:55

标签: javascript arrays shuffle

当我尝试对所有函数进行改组时,即使我为其他数组重写了该函数,也只能改组一个。我在网上寻找解决方案,并且在没有帮助的情况下尝试了所有解决方案,而且似乎没有任何效果。任何帮助将不胜感激。

'use strict'

window.onload = function () {
    let gameChoice = [[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8],
    ["a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g", "h", "h"],
    ["card.svg", "card1.svg", "card2.svg", "card.svg3", "card.svg4", "card5.svg", "card.svg6", "card.svg7"]];

    function shuffle(gameChoice) {
        let j, x, i;
        for (i = gameChoice.length - 1; i > 0; i--) {
            j = Math.floor(Math.random() * (i + 1));
            x = gameChoice[i];
            gameChoice[i] = gameChoice[j];
            gameChoice[j] = x;
        }
        return gameChoice;

    }

    let gameSelect = document.getElementById("input");

    gameSelect.addEventListener("click", function () {
        let options = gameSelect.querySelectorAll("option");
    });

    gameSelect.addEventListener("change", function () {
        if (gameSelect.value == "nothing") {
            return "";
        } else if (gameSelect.value == "numbers") {
            shuffle(array);
        } else if (gameSelect.value == "letters") {
            shuffle(array2);
        } else if (gameSelect.value == "colors") {
            shuffle(array3);
        };

        console.log(array, array2, array3);
    });
}

1 个答案:

答案 0 :(得分:0)

首先,我做了一系列的问候。您可以使用循环或作为索引对多个组执行此操作。我已经在手机上编译了代码,我将重新编写工作中可用的功能。

let gameChoice = [[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8],
    ["a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g", "h", "h"],
    ["card.svg", "card1.svg", "card2.svg", "card.svg3", "card.svg4", "card5.svg", "card.svg6", "card.svg7"]];

var a=gameChoice[0];
var b=gameChoice[1];
var c=gameChoice[2];
var newArray=[];
var newArray2=[];
var newArray3=[];
function shake()
{
var j = Math.floor(Math.random() * (a.length-1));

return j;}

function shake2()
{
var r = Math.floor(Math.random() * (b.length-1));

return r;}

function shake3()
{
var j = Math.floor(Math.random() * (c.length-1));

return j;}

for(i=0;i<16;i++)
{

var k=shake();
newArray.push(a[k]);

var x=shake2();

newArray2.push(b[x]);


a.splice(k,1);
b.splice(x,1);

}
for(r=0;r<8;r++)
{

var y=shake3();
newArray3.push(c[y]);
c.splice(y,1);

}

gameChoice[0]=newArray;
gameChoice[1]=newArray2;
gameChoice[2]=newArray3;
console.log(gameChoice[0],gameChoice[1],gameChoice[2]);

此输出

https://ibb.co/5hyY24Y

第二张图片。

https://ibb.co/g9MtDF7