【问题标题】:How can I select a random object from an array and then remove it so it doesn't get repeated?如何从数组中选择一个随机对象,然后将其删除,以免重复?
【发布时间】:2019-03-02 01:48:58
【问题描述】:

我是初学者 Javascript 学生,必须制作一个琐事游戏。我有一系列问题的对象。我想随机选择一个对象(问题)然后使用它,然后摆脱它,这样当我选择下一个问题时它就不会再次出现。我怎样才能正确地做到这一点?到目前为止我尝试过的是:

class Question
{
constructor(t,oA,oB,oC,oD,ans)
{
  this.title=t;
  this.optionA=oA;
  this.optionB=oB;
  this.optionC=oC;
  this.optionD=oD
  this.answer=ans;
}

  displayQuestion1R1()
 {
      userAnswer=prompt(`${this.title}\nA.${this.optionA}\nB.${this.optionB}\nC.${this.optionC}\nD.${this.optionD}`);
 }

}

Round1Questions.push(new Question("According to scientists, how old, 
approximately, is Earth?", "3 billions years", "100 million years", "4.5 
billion years","2.5 billion years", "4.5 billion years"));

Round1Questions.push(new Question("Who was the first American President?", 
"Benjamin Franklin", "Barack Obama", "George Washington","Thomas Jefferson", 
"George Washington"));

Round1Questions.push(new Question("How many Presidents have there been up to 
this year?(2019)?", "45", "40", "60","46", "45"));

Round1Questions.push(new Question("What is the largest Ocean in the world?", 
"Atlantic Ocean", "Pacific Ocean", "Indian Ocean","Arctic Ocean", "Pacific 
Ocean"));

Round1Questions.push(new Question("Which one of the following is not a 
Marvel super-hero?","Spider-Man","Hulk","Batman", "Iron Man", "Batman"));

let ri=RoundQuestions1[Math.floor(Math.random()*Round1Questions.length)];
let question1R1=Round1Questions.splice(ri, 1);

question1R1.displayQuestion1R1();

当我尝试运行它时,它说 question1R1.displayQuestion1R1() 不是一个函数。但是,如果我删除我拥有的拼接方法并且只是做 让 question1R1=RoundQuestions1[Math.floor(Math.random()*Round1Questions.length)]; 然后做 question1R1.displayQuestion1R1() 然后它工作。但是,这不允许我从数组中删除问题。我怎样才能做到这一点?

【问题讨论】:

标签: javascript arrays


【解决方案1】:

Ok Jack Bashford 也很接近,但拼接方法返回一个数组值。

class Question {
  constructor(t, oA, oB, oC, oD, ans) {
    this.title = t;
    this.optionA = oA;
    this.optionB = oB;
    this.optionC = oC;
    this.optionD = oD
    this.answer = ans;
  }

  displayQuestion1R1() {
  //  userAnswer = prompt(`${this.title}\nA.${this.optionA}\nB.${this.optionB}\nC.${this.optionC}\nD.${this.optionD}`);
    console.log( `${this.title}\nA.${this.optionA}\nB.${this.optionB}\nC.${this.optionC}\nD.${this.optionD}` )
  }
}

var Round1Questions = [];

Round1Questions.push(new Question("According to scientists, how old, approximately, is Earth ? ",
  "3 billions years ", "100 million years ", "4.5 billion years ","2.5 billion years ",
  "4.5 billion years ")
);

Round1Questions.push(new Question("Who was the first American President?",
  "Benjamin Franklin", "Barack Obama", "George Washington", "Thomas Jefferson",
  "George Washington")
);

Round1Questions.push(new Question("How many Presidents have there been up to  this year ? (2019) ? ",
  "45", "40", "60","46",
  "45")
);

Round1Questions.push(new Question("What is the largest Ocean in the world?",
  "Atlantic Ocean", "Pacific Ocean", "Indian Ocean", "Arctic Ocean",
  "Pacific Ocean ")
);

Round1Questions.push(new Question("Which one of the following is not a  Marvel super - hero ? ",
  " Spider-Man", "Hulk", "Batman", "Iron Man",
  "Batman ")
);

do {
  let
    PickQuestion_N = Math.floor(Math.random() * Round1Questions.length),
    PickedQuestion = Round1Questions.splice(PickQuestion_N, 1)[0]
  ;

  PickedQuestion.displayQuestion1R1();

} while (Round1Questions.length > 0)

【讨论】:

  • 啊,好吧,所以我试了一下,它奏效了。它与我的相似,只是你在“PickedQuestion = Round1Questions.splice(PickQuestion_N, 1)[0]”的末尾添加了“[0]”你能告诉我这是做什么的吗?
  • 正如我所说,Slice 方法返回一个数组,因为我们可以使用它来删除 1 或 X 个元素。这里你只想单独删除 1,这样就对应了零索引。
【解决方案2】:

问题

由于两个原因,您的方法不起作用:

  1. 您使用RoundQuestions1[Math.floor(Math.random()*Round1Questions.length)] 将您的Question 实例分配给r1 变量。然后,您在Round1Questions.splice(ri, 1) 中使用该实例,即not the expected argument type of Array.prototype.splice method。

  2. Round1Questions.splice(ri, 1) 返回一个数组。所以你的question1R1 是一个数组而不是一个问题。

解决方案

  1. 不要随机获取问题,而是尝试获取随机问题索引。
  2. 使用随机索引拼接题库。
  3. 获取从.splice 返回的数组的第一项。看看为什么here。

下面的工作代码(参见 cmets 的解释):

class Question {

  constructor(t, oA, oB, oC, oD, ans){
    this.title = t;
    this.optionA = oA;
    this.optionB = oB;
    this.optionC = oC;
    this.optionD = oD
    this.answer = ans;
  }

  displayQuestion1R1(){
    const { title, optionA, optionB, optionC, optionD } = this;
    const userAnswer = prompt(`
      ${title}
      A. ${optionA}
      B. ${optionB}
      C. ${optionC}
      D. ${optionD}
    `);
  }
  
}

const Round1Questions = [
  new Question(
    "According to scientists, how old, approximately, is Earth?",     "3 billions years",
    "100 million years",
    "4.5 billion years",
    "2.5 billion years",
    "4.5 billion years"
  ),
  new Question(
    "Who was the first American President?",
    "Benjamin Franklin",
    "George Washington",
    "Thomas Jefferson",
    "George Washington"
  ),
  new Question(
    "How many Presidents have there been up to this year (2019)?",
    "45",
    "40",
    "60",
    "46",
    "45"
  ),
  new Question(
    "What is the largest Ocean in the world?",
    "Atlantic Ocean",
    "Pacific Ocean",
    "Indian Ocean",
    "Arctic Ocean",
    "Pacific Ocean"
  ),
  new Question(
    "Which one of the following is not a Marvel super-hero?",
    "Spider-Man",
    "Hulk",
    "Batman",
    "Iron Man",
    "Batman"
  )
];

console.log(`Bank before prompt:`, Round1Questions.map(o => o.title));

// Instead of getting the question, get the index of question
let ri = randomQuestionIndex(Round1Questions);

// Use `ri` index to splice your question array.
// Notice the `[0]` to get the first item in the returned array
let question1R1 = Round1Questions.splice(ri, 1)[0];

// No error now because `question1R1` is a `Question` class instance
question1R1.displayQuestion1R1();

console.log(`Bank after prompt:`, Round1Questions.map(o => o.title));


function randomQuestionIndex(bank){
  return Math.floor(Math.random() * bank.length);
}
/* NOT RELATED TO YOUR CODE */
/* JUST TO MAKE CONSOLE MORE VISIBLE */
.as-console-wrapper { max-height: 100% !important; }

附言

不要在此处复制粘贴任何答案,因为您的教授会立即发现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2021-03-17
    • 2021-06-17
    • 2016-07-04
    • 2017-05-01
    • 2016-03-02
    相关资源
    最近更新 更多