2017-10-10 49 views
1

好吧,所以我認爲我是這樣做的,但事實上出了問題,因爲我的代碼永遠運行。請記住,我上高中並且對編碼很陌生,因此可能有一個非常簡單的解決方案,但我無法解決這個問題。如何檢查某個項目是否出現在數組中,並重新選擇新的項目?

我有一個數據文件,其中有各種問題數據。我的目標是從數據中隨機選擇一個問題,然後根據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語句來檢查問題ID是否出現在questionsAsked數組中。如果是這樣,我重新選擇另一個問題。如果沒有,則將id推入數組中,並返回問題。

我的問題是,如果if語句爲true,則會選擇另一個問題,但不會再次檢查它是否出現在questionsAsked數組中。也嘗試了一段時間循環,看看是否有什麼區別,但那是當我的代碼卡在一個無限循環。

while (questionsAsked.includes(guID)) { 
    let selectedQuestion = keys[Math.floor(Math.random() * keys.length)]; 
    let questionID = (selectedQuestion.questionID); 
} 

questionsAsked.push(guID); 
return selectedQuestion; 

任何建議將非常大大appriciated

回答

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>

相關問題