My program works for one user question but I want to include many questions. For example the question in my program asks the user to arrange in correct order the phrase, 'How are you today?', but how can I include others, 'What would you like to eat for breakfast?', etc..
我的程序适用于一个用户问题,但我想包含很多问题。例如,我的程序中的问题要求用户按照正确的顺序排列“今天你好吗?”这句话,但我怎样才能包括其他人,“你想吃早餐吃什么?”等等。
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>
1 个解决方案
#1
Try the below.
试试下面的内容。
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>
#1
Try the below.
试试下面的内容。
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>