Skip to content Skip to sidebar Skip to footer

Using Javascript To Randomly Select 10 Exercises From Exercise Bank (json)

The 'exercises' in the title refers to questions, but StackOverflow does not allow the word 'question' in the title. I'll start out by saying that I am very much a newbie when it c

Solution 1:

Sort the array randomly and take a slice of n items from it would also do the trick:

yourArrayOfItems.sort(() =>Math.random() - 0.5).slice(0, 10);
//                    ^ random sort method       ^ take 10 items

Solution 2:

So, let's have a TLDR of your aim.

You have an array of 100 questions. You would like to have an other array of 10 questions selected from the first array so as in the resulting array there're no repeating questions.

How would we do it?

You should pick a random index within the array bounds 10 times, and each time check if you haven't picked that index already (avoid duplicates, in other words).

The realisation.

I suppose that you have this object stored in some variable. If you have got the input as string which is a valid JSON, you could use JSON.parse().

var obj = {"questionlist":[..., ..., ...]}

First of all, we get the array of all the questions by the questionList key.

var list = obj.questionlist;

Now let's create the array to contain the resulting 10 questions.

var resultList;

We are going to populate it by pushing 10 times a random index that would be >=0 and <list.length, and not forgetting to make sure it's a unique index every time we push.

First let's decide how we pick a random index.

The random float within [0,1] can be received by executing Math.random(). Now to get the number within 0 ... list.length we just multiply it by the list.length. Not forgetting it could be a float, so making it an integer by throwing away the part after the floating point (i.e., floor the number). The resulting formula is ~~(Math.random()*list.length),

Let's write all the code now.

for( var k = 0; k < 10; k++ ) {
    var randIndex = ~~(Math.random()*list.length);
    //check if the index is not duplicateif( resultList.indexOf(randIndex) < 0)
        resultList.push(randIndex);
    else// else the index is duplicate so we don't need it and should move one step back in the loop
        k--;
}

Now the resultList contains 10 random non-repeating question indexes. You could now output the resulting list of 10 random questions by looping the resultList array.

for( var k = 0; k < resultList.length; k++ )
    console.log(list[resultList[k]]);

Solution 3:

Please try this, where 'n' is the number of random elements you want,

const items = {
    "questionlist": [{
            "question": "Test question 1",
            "option1": "Answer1A",
            "option2": "Answer1B",
            "option3": "Answer1C"
        },
        {
            "question": "Test question 2",
            "option1": "Answer2A",
            "option2": "Answer2B",
            "option3": "Answer2C"
        },
        {
            "question": "Test question 3",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        },
        {
            "question": "Test question 4",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        },
        {
            "question": "Test question 5",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        },
        {
            "question": "Test question 6",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        },
        {
            "question": "Test question 7",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        },
        {
            "question": "Test question 8",
            "option1": "Answer3A",
            "option2": "Answer3B",
            "option3": "Answer3C"
        }
    ]
}

functionrandom_item(questions, n) {
    var arr = [];
    for (let i = 0; i < n; i++) {
        arr.push(questions[Math.floor(Math.random() * questions.length)])
    }
    return arr

}

console.log(random_item(items.questionlist, 2))

Solution 4:

This function selects N questions out of an array by random:

var bank = {
  "questionlist": [{
      "question": "Test question 1",
      "option1": "Answer1A",
      "option2": "Answer1B",
      "option3": "Answer1C"
    },
    {
      "question": "Test question 2",
      "option1": "Answer2A",
      "option2": "Answer2B",
      "option3": "Answer2C"
    },
    {
      "question": "Test question 3",
      "option1": "Answer3A",
      "option2": "Answer3B",
      "option3": "Answer3C"
    }
  ]
}

functionselectN(bank, n) {
  var list = bank.questionlist;
  if (list.length < n) n = list.length; //to prevent endless loopvar set = newSet(); //using a set to prevent duplicationwhile (set.size < n) set.add(list[Math.floor(Math.random() * list.length)]);
  returnArray.from(set)
}

console.log(selectN(bank, 2));

Solution 5:

You can do it like this.

Math.random() is a function, which gives a floating point number between 0 and 1.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

Math.floor() takes a floating point number, and rounds it down to the next full integer.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

var list = {"questionlist":[
  {
  "question":"Test question 1",
  "option1":"Answer1A",
  "option2":"Answer1B",
  "option3":"Answer1C"
  },
  {
  "question":"Test question 2",
  "option1":"Answer2A",
  "option2":"Answer2B",
  "option3":"Answer2C"
  },
  {
  "question":"Test question 3",
  "option1":"Answer3A",
  "option2":"Answer3B",
  "option3":"Answer3C"
  }
]
}

list = list.questionlistconsole.log(
    list[Math.floor(Math.random() * list.length)]['question']
);

Post a Comment for "Using Javascript To Randomly Select 10 Exercises From Exercise Bank (json)"