gpt4 book ai didi

javascript - 如何从数组中选择一个随机对象然后将其删除以免重复?

转载 作者:行者123 更新时间:2023-12-01 01:13:35 25 4
gpt4 key购买 nike

我是 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() 然后它就可以工作了。然而,这不允许我从数组中删除这个问题。我怎样才能做到这一点?

最佳答案

好吧,Jack Bashford 也很接近,但是 splice 方法返回一个数组值。

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)

关于javascript - 如何从数组中选择一个随机对象然后将其删除以免重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54954499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com