1

So i was making a discord bot and decided to implement a simple trivia api, upon returning the results it shows this :

[
  {
    category: 'Entertainment: Video Games',
    type: 'multiple',
    difficulty: 'medium',
    question: 'What is the main character of Metal Gear Solid 2?',
    correct_answer: 'Raiden',
    incorrect_answers: [ 'Solidus Snake', 'Big Boss', 'Venom Snake' ]
  }
]

since this is my current code

 //creates an embed to make it more aestheticaly pleasing
    const triviaEmbed = new Discord.MessageEmbed()
      .setColor(0x6e7175)
      .setTitle('Trivia!')
      .addFields({
        name: 'Category',
        value: triviaValue.results[0].category
      }, {
        name: 'Type',
        value: triviaDisplayType
      }, {
        name: 'Difficulty',
        value: triviaDisplayDifficulty
      }, {
        name: 'Question',
        value: triviaValue.results[0].question
      }, {
        name: 'Answers',
        value: `${triviaValue.results[0].incorrect_answers}, ${triviaValue.results[0].correct_answer}`
      })
    // sends the embed
    message.channel.send(triviaEmbed);

the correct answer will always be the last one, what is the most efficient way to make this process random, the best things i could think of seemed very redundant.

iris87
  • 103
  • 1
  • 2
  • 11

3 Answers3

2

As incorrect_answers is an array, you can create a copy of it and insert the correct answer in this new array then use an array shuffle algorithm to shuffle it out.

We can use ES6 spread operator to copy the incorrect_answers easily. If you want to know more about it, click here.

// Creates an array with the values of incorrect answers + the correct answer
const result = triviaValue.results[0];
const answers = [...result.incorrect_answers, result.correct_answer];

You can see some examples of array shuffling algorithms clicking here.

For this example, we will be using the Durstenfeld shuffle found in the link. I'd recommend you creating a utils folder and having it as a module so you could use it whenever you need it. But to not over complicate things I'll show you an example code with the function above.

Here's how you can do it:

// Declare the array shuffler - best if it was a module and you
// got it by using require() or import
function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    let temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

// Generate the array
const result = triviaValue.results[0];
const answers = [...result.incorrect_answers, result.correct_answer];

// Shuffle It
shuffleArray(answers);

// Create the Embed
const triviaEmbed = new Discord.MessageEmbed()
  .setColor(0x6e7175)
  .setTitle('Trivia!')
  .addFields({
    name: 'Category',
    value: triviaValue.results[0].category
  }, {
    name: 'Type',
    value: triviaDisplayType
  }, {
    name: 'Difficulty',
    value: triviaDisplayDifficulty
  }, {
    name: 'Question',
    value: triviaValue.results[0].question
  }, {
    name: 'Answers',
    // this will break the line for every answer
    // you could go ahead and use ', ' as parameter
    // if you wish them to be in a single line
    value: answers.join('\n')
  });

// Send the embed
message.channel.send(triviaEmbed);

Gabriel Andrade
  • 684
  • 4
  • 8
0

You can try push the correct answer at a random point in incorrect answers.

Example:

const answers = triviaValue.results[0].incorrect_answers
answers.splice(Math.floor(Math.random() * answers), 0, triviaValue.results[0].correct_answer)

The code use splice method to insert the correct answer into answers array, and Math.floor(Math.random() * answers) just pick a random index to insert.

Guaxinim
  • 304
  • 1
  • 9
-1

How about putting all the answers (including the correct one) into one array called answers, and then have an integer that would represent the index (0 to 3) of the correct answer?

That would be the simplest approach, IMO.

reobin
  • 59
  • 1
  • 2