1

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. However, I want the 0th index to be excluded, as this is the correct answer and will always be added separately.

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. Apologies in advance if it's an accessible solution, I might not be wording my question well.

burner1up
  • 49
  • 4
  • Any code attempt? You mention a goal for "random"... JS has a random method under the global object `Math`. The rest is just knowing how to index a JS array. – GetSet Dec 21 '20 at 01:02
  • 1
    Does this answer your question? [Random number generator without dupes in Javascript?](https://stackoverflow.com/questions/3796786/random-number-generator-without-dupes-in-javascript) – Bryan Dellinger Dec 21 '20 at 01:04
  • Or: [How to randomize (shuffle) a JavaScript array?](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) shuffle and take the first three. – pilchard Dec 21 '20 at 01:10

4 Answers4

2

The following should do it for you. slice(1) will return an array without the first element. The sort method will generally randomize the sorting of the array. Finally, slice(0, 3) will grab the first three elements of the randomly sorted array.

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);
Nick
  • 16,066
  • 3
  • 16
  • 32
1

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))
Automatico
  • 12,420
  • 9
  • 82
  • 110
0

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);
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

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. Next , store two minus the length of answers in a variable. Then , get a random number from 1 to the variable you used earlier. 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.

Ethan Eyob
  • 19
  • 4