简体   繁体   English

如何在javascript中修改数组并提取随机值

[英]how to modify array in javascript and pull random value

I want to build a simple quiz app for geography class, my logic is this: store the question into an array and pull a random question from an array, when the user clicks on the answer a function fires up and compare the answer, after answer exclude question from the array and pull another random question so far I've managed to pull random question, give an answer, compare answer, and remove the question from the array and pull another random question but when function fires up the answer is compared with the first random question我想为地理课构建一个简单的测验应用程序,我的逻辑是:将问题存储到一个数组中并从一个数组中提取一个随机问题,当用户点击答案时,一个函数会启动并比较答案,然后回答从数组中排除问题并拉出另一个随机问题到目前为止我已经设法拉出随机问题,给出答案,比较答案,然后从数组中删除问题并拉出另一个随机问题,但是当函数启动时,答案与第一个随机问题

here is y code so far:到目前为止,这是y代码:


    let judet = judete[Math.floor(Math.random() * judete.length)];

    console.log("start> "+judet);

    function checkAnswer(val){

        console.log("ras= "+val)


        if(val==judet){
            console.log("corect");


            let valueToRemove = val;
            let filteredItems = judete.filter(item => item !== valueToRemove);
            let judet = judete[Math.floor(Math.random() * judete.length)];

            console.log("inrebare= "+judet);

        }else{
            console.log("gresit");

            let valueToRemove = val;
            let filteredItems = judete.filter(item => item !== valueToRemove);
            let judet = judete[Math.floor(Math.random() * judete.length)];

            console.log("inrebare= "+judet);

        }


    }

As @Dave said in the comments, you can shuffle the array and then pop.正如@Dave 在评论中所说,您可以对数组进行洗牌,然后弹出。

 var arr = [1, 2, 3, 4, 5]; for (var i = 0; i < 5; i++) { shuffle(arr); console.log(arr.pop()); } function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { let j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } }
 Keep re-running me to see the result.

@Saud you pointed me into the right direction, here is how i have done it: @Saud 你给我指出了正确的方向,这是我的做法:

let judete = ["ar","ab","ag","tm","bh"];

    shuffle(judete);


    function shuffle(array) {
    for (let i = array.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
     }
    }
        let judet = judete.pop();


    console.log("start> "+judet);  

    function checkAnswer(val){

        console.log("ras= "+val);


        if(val==judet){
            console.log("corect");

            shuffle(judete); 
            judet = judete.pop();

            console.log("inrebare= "+judet);

        }else{
            console.log("gresit");

            shuffle(judete); 
            judet = judete.pop();

            console.log("inrebare= "+judet);

        }


    }

Why not use randojs.com ?为什么不使用randojs.com

var arr = randoSequence(["question 1", "question 2", "question 3"]);
for(var i = arr.length - 1; i >= 0; i--){
  console.log(arr.pop().value);
}

Just include this in your head tag if you wanna use my answer:如果您想使用我的答案,只需将其包含在您的头部标签中:

<script src="https://randojs.com/1.0.0.js"></script>

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

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