对象内部的对象如何生成随机项?

时间:2019-10-30 10:26:25

标签: javascript

我有一个要随机生成的对象,这是一个问题测验。我试过var

index = Math.floor(Math.random() *currentQuestion.answers[letter]);

没有运气,任何人都可以帮助并解释为什么我不能随机生成该项目吗?

我需要在我的createQuiz函数中生成随机答案,现在固定为exp:A:MJ,B:Pippen,C:Magic,如果刷新,它将随机生成A:Pippen B:Magic C:MJ等等

我的对象变量

 const myQuestions = [
{
 question: "What's my name ?",
 answers: {
  item1: "Chris",
  item2: "Leborn",
  item3: "Webber"
 },
 correctAnswer: "Chris",
 button: "Next"
},

{
 question: "What's my age ?",
 answers: {
  item1: "31",
  item2: "30",
  item3: "29"
 },
 correctAnswer: "31",
 button: "Next"
},

{
 question: "What's my favor NBA star ?",
 answers: {
  item1: "MJ",
  item2: "Pippen",
  item3: "Magic"
 },
 correctAnswer: "MJ",
 button: "Done"
}
]

功能

function createQuiz() {
  //clear the contents of questions div first 
  document.getElementById('questionsBox').innerHTML = "";

  //clear answers box
  document.getElementById('answersBox').innerHTML = "";


  //set answer sting
  answersCaptcha = [];

  //output
  output = [];

  // for each question...
  myQuestions.forEach(
    (currentQuestion, questionNumber) => {

  // we'll want to store the list of answer choices
  const answers = [];
  const option = ["A","B","C"];
  let count = -1;

  for(letter in currentQuestion.answers){
  count++;

  var index = Math.floor(Math.random() * currentQuestion.answers[letter]);

  // i need to generate random answers item here, now is fixed
  // exp: A: MJ, B:Pippen, C:Magic and if refresh it will randomly generate A:Pippen B:Magic C:MJ and so on.


  answers.push(
  `<label>
  <input type="radio" name="question${questionNumber}" value="${letter}">
  ${option[count]} :
  ${currentQuestion.answers[letter]}
  </label>`
  );
  }


  // add this question and its answers to the output
     output.push(
       `<div class="question"> ${currentQuestion.question} </div>
       <div class="answers"> ${answers.join('')} </div>`
     ); 

  })

  document.getElementById('answersBox').innerHTML = output.join('');
  console.log(answersCaptcha);

}

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题...您所需要的只是随机播放(currentQuestion.answers)

答案 1 :(得分:0)

问题是字母不是数字-不能用于随机化。这是一个解决方案:

const myQuestions = [{
    question: "What's my name ?",
    answers: {
      item1: "Chris",
      item2: "Leborn",
      item3: "Webber"
    },
    correctAnswer: "Chris",
    button: "Next"
  },

  {
    question: "What's my age ?",
    answers: {
      item1: "31",
      item2: "30",
      item3: "29"
    },
    correctAnswer: "31",
    button: "Next"
  },

  {
    question: "What's my favor NBA star ?",
    answers: {
      item1: "MJ",
      item2: "Pippen",
      item3: "Magic"
    },
    correctAnswer: "MJ",
    button: "Done"
  }
]

createQuiz()

function createQuiz() {
  //clear the contents of questions div first 
  document.getElementById('questionsBox').innerHTML = "";

  //clear answers box
  document.getElementById('answersBox').innerHTML = "";


  //set answer sting
  answersCaptcha = [];

  //output
  output = [];

  // for each question...
  myQuestions.forEach(
    (currentQuestion, questionNumber) => {

      // we'll want to store the list of answer choices
      const option = ["A", "B", "C"];
      let count = -1;

      const answers = shuffleArray(Object.entries(currentQuestion.answers)).map((e, i) => {
        return `<label>
  <input type="radio" name="question${questionNumber}" value="${e[0]}">
  ${option[i]} :
  ${currentQuestion.answers[e[0]]}
  </label>`
      })


      // add this question and its answers to the output
      output.push(
        `<div class="question"> ${currentQuestion.question} </div>
       <div class="answers"> ${answers.join('')} </div>`
      );

    })

  document.getElementById('answersBox').innerHTML = output.join('');
  console.log(answersCaptcha);

}

function shuffleArray(arr) {
  let array = arr
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array
}
<div id="questionsBox"></div>
<div id="answersBox"></div>