-4

So basically, I am making a trivia with random questions, but I have no idea how to put 2 "actions" in a single random choice. I managed to put the question (For the sake of this question I've replaced the questions with "Question").

Here is the code:

if (message.content.startsWith(prefix + 'random')) {
 questions = [
  'Question1',
  'Question2',
  'Question3',
  'Question4',
  'Question5',
  'Question6',
  'Question 7',
  'Question 8',
  'Question9',
  'Question10',
 ];
 question = Math.floor(Math.random() * questions.length);
 var embed = new Discord.MessageEmbed()
  .setTitle(':dvd: Question')
  .setDescription(invisible)
  .addField(questions[question], invisible) //Here I put a random question
  .addField(
   'Here I want to put the answer choices',
   'Here I want to put the answer choices'
  ) // And here I would want to put the appropriate answer choices
  .setColor('#f5980c')
  .addField(invisible, field)
  .setFooter(footer);
 message.channel.send(embed);
 console.log(` ${message.author.tag} used Question`);
}
Lioness100
  • 7,858
  • 5
  • 13
  • 46

2 Answers2

0

You can use arrays within an array.

var questionsAndAnswers = [
  ["What's 1+1?", "2"],
  ["What's 2+2?", "4"],
  ["What's 3+3?", "6"],
  ["What's 4+4?", "8"],
  ["What's 5+5?", "10"],
  // as many questions and answers as you want...
];

const random = questionsAndAnswers[Math.floor(Math.random() * questionsAndAnswers.length)];

const question = random[0]; // first element in the array is the question
const answer = random[1]; // second is the answer

console.log('Your Question:', question);
console.log('The Answer:', answer);

Another example:

// store all data within nested arrays
const options = [
  ['Today we will', 'Be baking the', 'Muffins'],
  ['Tomorrow we will', 'Be eating the', 'Muffins'],
  ['Yesterday we bought', 'Ingredients for the', 'Muffins']
];

const random = options[Math.floor(Math.random() * options.length)];

console.log(random[0]); // so that 
console.log(random[1]); // every value
console.log(random[2]); // goes together
Lioness100
  • 7,858
  • 5
  • 13
  • 46
0

A kind of intuitive, customary representation of quiz data is to associate questions and answers...

// a "problem" is a question, possible answers, and the correct answer...
let problemA = {
  question: "What is the meaning of life, the universe and everything?",
  answers: [
    "That's too profound", "It's all about love", "42", "It's all about money"
  ],
  correctAnswerIndex: 2
}

let problemB = {...}
let problemSet = [problemA, problemB, ...]

Another issue is choosing a problem randomly to present to the user. The OP's approach of picking a random index is okay, but will result in repeated problems being presented.A better solution is to "shuffle" the problem set like a deck of cards then present one at time from the shuffled deck. A simple, fast shuffle is called Fisher-Yates.

let problemSet = [problemA, problemB, ...]
let shuffledProblemSet = shuffle(problemSet) // fisher-yates shuffle

// to present a "random" non-repeating problem, just traverse the shuffled problems in order
let index = 0

let nextProblem = shuffledProblemSet[index++]
// now, send this to discord...

const embed = new Discord.MessageEmbed()
  .addField(nextProblem.question, invisible) //Here I put a random question
  .addField(nextProblem.answers) // And here I would want to put the appropriate answer choices
  // yada yada
danh
  • 59,538
  • 10
  • 90
  • 129