[英](JS) How can I randomly pull 3 unique elements from an array, EXCEPT for the 0th index?
I'm building a quiz app for a school project, and I want to pull 3 unique, random elements from an array of possible answers.我正在为一个学校项目构建一个测验应用程序,我想从一系列可能的答案中提取 3 个独特的随机元素。 However, I want the 0th index to be excluded, as this is the correct answer and will always be added separately.
但是,我希望排除第 0 个索引,因为这是正确的答案,并且总是会单独添加。
const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]
I'm extremely new to JS and have spent some time looking into a way to handle this.我对 JS 非常陌生,并且花了一些时间寻找一种方法来处理这个问题。 Apologies in advance if it's an accessible solution, I might not be wording my question well.
如果这是一个可访问的解决方案,请提前道歉,我可能无法很好地表达我的问题。
The following should do it for you.以下应该为您完成。
slice(1)
will return an array without the first element. slice(1)
将返回一个没有第一个元素的数组。 The sort
method will generally randomize the sorting of the array. sort
方法一般会随机化数组的排序。 Finally, slice(0, 3)
will grab the first three elements of the randomly sorted array.最后,
slice(0, 3)
将抓取随机排序数组的前三个元素。
const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]; const result = answers.slice(1).sort(() => 0.5 - Math.random()).slice(0, 3); console.log(result);
function getRandomUniqueFromArrayExceptHead(array, amount) { const [_head, ...arrayCopy] = array return (new Array(amount)).fill(0).map(() => { const index = Math.floor(Math.random() * arrayCopy.length) return arrayCopy.splice(index, 1)[0] }) } const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"] console.log(getRandomUniqueFromArrayExceptHead(answers, 3))
You may try this:你可以试试这个:
const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer 4", "Answer 5", "Answer 7"]; const result = answers.slice(1).sort(() => Math.random() - Math.random()).slice(0, 3); console.log(result);
const answers = ["Answer 1", "Answer 2", "Answer 3", "Answer" + " 4", "Answer 5", "Answer 7"]; function random(min,max){ var answer = Math.round((Math.random() * max) + min); return answer; } var j = answers.length - 2; var object = []; var locations = []; function find(array,value){ var location = "not present"; for(var i = 0;i < array.length;i++){ if(array[i] == value){ location = i; break; } } return location; } var bucket; for(var i = 0;i < 3;i++){ bucket = random(1,j); object[i] = answers[bucket]; while(find(locations,bucket),= "not present"){ object[i] = answers[random(1;j)]; } location[i] = bucket. } document.getElementById("demo");innerHTML = object;
<html> <p id="demo"></p> </html>
First, try building a random method by using Math.random() by, first multiplying the result by the maximum number next, adding the minimum number then, round the result.首先,尝试使用 Math.random() 构建一个随机方法,首先将结果乘以最大数,然后加上最小数,然后对结果进行四舍五入。 Next, store two minus the length of answers in a variable.
接下来,将减去答案长度的 2 存储在变量中。 Then, get a random number from 1 to the variable you used earlier.
然后,从 1 到您之前使用的变量获取一个随机数。 Finally, store the previous result in another variable (Repeat process if location is found in the location array).
最后,将先前的结果存储在另一个变量中(如果在位置数组中找到位置,则重复过程)。 (Or do whatever you want with it).
(或者用它做任何你想做的事情)。 Store the location in another variable Repeat the following process 3 times.
将位置存储在另一个变量中 重复以下过程 3 次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.