简体   繁体   English

使用数学随机后从数组中删除使用过的元素

[英]Remove used element from array after using math random

I am trying to work on quiz with multiple answers but I don't want to display the same questions twice, as there are 4 possible answers.我正在尝试使用多个答案进行测验,但我不想两次显示相同的问题,因为有 4 个可能的答案。 How can I delete an item from an array after using math random?使用数学随机后如何从数组中删除项目?

var answers = ['apple', 'banana', 'orange', 'kiwi'];
      document.getElementById("firstOption").innerHTML = answers[Math.floor(Math.random() * answers.length)];
      document.getElementById("secondOption").innerHTML = answers[Math.floor(Math.random() * answers.length)];
      document.getElementById("thirdOption").innerHTML = answers[Math.floor(Math.random() * answers.length)];
      document.getElementById("fourthOption").innerHTML = answers[Math.floor(Math.random() * answers.length)];

So I want 4 different options, but this is just randomly picking values from the array and sometimes are the same.所以我想要 4 个不同的选项,但这只是从数组中随机选取值,有时是相同的。

just shuffle the answers array, and apply the values in order to the nodes只需打乱答案数组,然后将值应用于节点

 function shuffle(arr){ for(let i=arr.length, j, tmp; i > 0; ){ tmp = arr[j = Math.floor(Math.random() * i--)]; arr[j] = arr[i]; arr[i] = tmp; } return arr; } function mix(){ var answers = shuffle(['apple', 'banana', 'orange', 'kiwi']); [...document.querySelectorAll('#firstOption, #secondOption, #thirdOption, #fourthOption')] .forEach(function(node, index){ node.textContent = answers[index]; }); } mix();
 <ul> <li id="firstOption"></li> <li id="secondOption"></li> <li id="thirdOption"></li> <li id="fourthOption"></li> </ul> <input type="button" onclick="mix()" value="mix"/>

Just swap current randomly selected question w last index of current random range只需用当前随机范围的最后一个索引交换当前随机选择的问题

Here the implementation for above algo这里是上述算法的实现

var answers = ['apple', 'banana', 'orange', 'kiwi'];
let currentLastIndex = 4;
let random;
while(last > 0) {
    random = Math.floor(Math.random() * currentLastIndex--);
    console.log(answers[random]); //replace this with required code
    swap(answers, currentLastIndex, random)
}
function swap(arr, firstIndex, secondIndex) {
    let temp = answers[random];
    answers[random] = answers[last];
    answers[last] = temp;
}

sample run 
['apple', 'banana', 'orange', 'kiwi'] //question array
let random = 1 //banana selected
['apple', 'kiwi', 'orange', 'banana'] //replace banana with kiwi
let random = 1 //kiwi selected
['apple', 'orange', 'kiwi', 'banana'] //replace kiwi with orange
let random = 0 //apple selected
['orange', 'apple', 'kiwi', 'banana'] //replace apple with orange
let random = 0 //orange selected
['apple', 'banana', 'orange', 'kiwi'] //orange apple with orange
//done 

The easiest way to do this would be to sort the array randomly / shuffe the array before writing to DOM.最简单的方法是在写入 DOM 之前对数组进行随机排序/对数组进行洗牌。

Hit run snippet repeatedly to see the random order of items.反复点击运行片段以查看项目的随机顺序。

 var answers = ['apple', 'banana', 'orange', 'kiwi']; var el = document.getElementById('options'); answers .sort(shuffle) .forEach(function(item) { var option = document.createElement('div'); option.innerHTML = item; el.appendChild(option); }); function shuffle() { return Math.random() - Math.random(); }
 <div id="options"> </div>

Try using splice method as soon to remove particular element from the array once it has been used.尝试使用 splice 方法在使用后立即从数组中删除特定元素。

Sample code:示例代码:

var answers = ['apple', 'banana', 'orange', 'kiwi'];
var randomElement = answers[Math.floor(Math.random() * answers.length)];
document.getElementById("firstOption").innerHTML = randomElement;
var index = answers.indexOf(randomElement);
answers.splice(index, 1);

Find complete code here .在这里找到完整的代码。

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

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