好的,所以我认为我这样做是对的,但显然出现了问题,因为我的代码永远存在。请记住我在高中和新编码所以可能有一个非常简单的解决方案,但我无法解决。
我有一个数据文件,其中包含各种问题数据。我的目标是从数据中随机选择一个问题,然后根据id的数组检查问题的ID。如果id出现在该数组中,我想重新运行随机选择功能来选择另一个问题。我想继续这样做,直到我有一个尚未出现的问题。
var dataFile = {
'01': { questionID: '00100A', text: 'Random question number one?'},
'02': { questionID: '00200B', text: 'Random question number two?'},
'03': { questionID: '00300C', text: 'Random question number three?'},
'04': { questionID: '00400D', text: 'Random question number four?'},
'05': { questionID: '00500E', text: 'Random question number five?'},
'06': { questionID: '00600F', text: 'Random question number six?'},
'07': { questionID: '00700G', text: 'Random question number seven?'},
'08': { questionID: '00800H', text: 'Random question number eight?'},
'09': { questionID: '00900I', text: 'Random question number nine?'},
'10': { questionID: '01000J', text: 'Random question number ten?'}};
var questionsAsked = ['00100A', '00300C', '00500E', '00700G', '00900I'];
const keys = Object.keys(dataFile).map(key => dataFile[key]);
let selectedQuestion = keys[Math.floor(Math.random() * keys.length)];
let questionID = (selectedQuestion.questionID)
if (questionsAsked.includes(questionID)) {
let selectedQuestion = keys[Math.floor(Math.random() * keys.length)];
return selectedQuestion;
} else {
questionsAsked.push(questionID);
return selectedQuestion;
}
数据文件中有9个数据。还有一系列问题已被提出。我首先映射dataFile以获取问题数据,然后从中选择一个随机问题。我从该问题中提取questionID,然后使用if ... else语句来检查questionID是否出现在questionsAsked数组中。如果是这样,我重新选择另一个问题。如果没有,则将id推入数组并返回问题。
我的问题是,如果if语句为true,则选择另一个问题但从未再次检查以查看它是否出现在questionsAsked数组中。还尝试了一个while循环,看看是否有任何区别,但那是当我的代码陷入无限循环时。
while (questionsAsked.includes(guID)) {
let selectedQuestion = keys[Math.floor(Math.random() * keys.length)];
let questionID = (selectedQuestion.questionID);
}
questionsAsked.push(guID);
return selectedQuestion;
任何建议都非常适合
答案 0 :(得分:0)
看看这个解决方案:
var dataFile = {
'01': { questionID: '00100A', text: 'Random question number one?'},
'02': { questionID: '00200B', text: 'Random question number two?'},
'03': { questionID: '00300C', text: 'Random question number three?'},
'04': { questionID: '00400D', text: 'Random question number four?'},
'05': { questionID: '00500E', text: 'Random question number five?'},
'06': { questionID: '00600F', text: 'Random question number six?'},
'07': { questionID: '00700G', text: 'Random question number seven?'},
'08': { questionID: '00800H', text: 'Random question number eight?'},
'09': { questionID: '00900I', text: 'Random question number nine?'},
'10': { questionID: '01000J', text: 'Random question number ten?'}};
var questionsAsked = ['00100A', '00300C', '00500E', '00700G', '00900I'];
const keys = Object.keys(dataFile).map(key => dataFile[key]);
//wrapped it in a function
function randomQuestion()
{
let selectedQuestion = keys[Math.floor(Math.random() * keys.length)];
let questionID = (selectedQuestion.questionID)
//if the question does not exists in the array indexOf will return -1
//so push the answer into the array and return selected
if (questionsAsked.indexOf(questionID) === -1) {
questionsAsked.push(questionID);
return selectedQuestion;
} else {
//if it does exist make it recursive and run again until it does.
if (questionsAsked.length < keys.length)
{
return randomQuestion();
}
else {
return {"message" : "There are no more random questions."};
}
}
}
document.querySelector("button").addEventListener("click", function(){
console.log(randomQuestion(), questionsAsked);
});
<button>role it</button>