如何创建多个用户问题?

时间:2015-05-06 12:58:59

标签: javascript html

我的程序适用于一个用户问题,但我想提出很多问题。例如,我的程序中的问题要求用户按正确的顺序排列短语“今天你好吗?”#39;但是我怎样才能包含其他人,'你想吃什么?早餐?'等..



    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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分: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>