2015-05-06 28 views
0

我的程序適用於一個用戶的問題,但我想包括很多問題。例如,我的程序中的問題要求用戶按正確的順序排列短語「今天過得如何?」,但我怎麼能包括其他人,「你想吃什麼早餐?」等。我如何着手創建多個用戶問題?

var words = ['how', 'are', 'you', 'today?']; 
 
    var correctInput = "how are you today?"; 
 
    var userInput = 'how are you today?'; 
 
    var newWords = words.slice(0); 
 
    shuffle(newWords); 
 
    question(); 
 

 
    function question() { 
 
     var el = document.getElementById('phrase'); 
 
     el.textContent = newWords.join(' '); 
 
     document.getElementById("myForm").onsubmit = checkAnswer;} 
 

 
    function checkAnswer() { 
 
     var elMsg = document.getElementById('feedback'); 
 
     if (document.myForm.textinput.value == correctInput) { 
 
     elMsg.textContent = "correct"; 
 
     } else { 
 
      elMsg.textContent = "wrong answer";} 
 
     return false;} 
 

 
     function shuffle(newWords) { 
 
      var counter = newWords.length, temp, index; 
 
      while (counter > 0) { 
 
       index = Math.floor(Math.random() * counter); 
 
       counter--; 
 
       temp = newWords[counter]; 
 
       newWords[counter] = newWords[index]; 
 
       newWords[index] = temp;} 
 
       return newWords;}
<form name="myForm" id="myForm"> 
 
     <div id ="phrase"></div>  
 
     <input type = "text" id = "textinput" /> 
 
     <button>Click here</button> 
 
     <div id ="feedback"></div> 
 
    </form>

回答

0

嘗試以下。

var words = [ 
 
    ['how', 'are', 'you', 'today?'], 
 
    ['What', 'would', 'you', 'like', 'to', 'eat', 'for', 'breakfast?'] 
 
]; 
 
var correctInput = [ 
 
    ["how are you today?"], 
 
    ["What would you like to eat for breakfast?"] 
 
]; 
 
var i = -1; 
 
nextQuestion(); 
 

 
function question() { 
 
    var el = document.getElementById('phrase'); 
 
    el.textContent = newWords.join(' '); 
 
    document.getElementById("myForm").onsubmit = checkAnswer; 
 
} 
 

 
function checkAnswer() { 
 
    var elMsg = document.getElementById('feedback'); 
 
    if (document.myForm.textinput.value == correctInput[i]) { 
 
    elMsg.textContent = "correct"; 
 
    nextQuestion(); 
 
    } else { 
 
    elMsg.textContent = "wrong answer"; 
 
    } 
 
    return false; 
 
} 
 

 
function shuffle(newWords) { 
 
    var counter = newWords.length, 
 
    temp, index; 
 
    while (counter > 0) { 
 
    index = Math.floor(Math.random() * counter); 
 
    counter--; 
 
    temp = newWords[counter]; 
 
    newWords[counter] = newWords[index]; 
 
    newWords[index] = temp; 
 
    } 
 
    return newWords; 
 
} 
 

 
function nextQuestion() { 
 
    if (i < words.length-1) { 
 
    i++; 
 
    } else { 
 
    i = 0; 
 
    } 
 
    newWords = words[i].slice(0); 
 
    shuffle(newWords); 
 
    question(); 
 
}
<form name="myForm" id="myForm"> 
 
    <div id="phrase"></div> 
 
    <input type="text" id="textinput" /> 
 
    <button>Click here</button> 
 
    <div id="feedback"></div> 
 
</form>

+0

謝謝Pugazh,我現在明白了。 – user3739794