【发布时间】: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() 然后它工作。但是,这不允许我从数组中删除问题。我怎样才能做到这一点?
【问题讨论】:
-
您应该只考虑shuffling the array,然后
popping 数组中的项目。
标签: javascript arrays