Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从两个javascript数组创建一个简单的测验_Javascript_Php_Html_Arrays - Fatal编程技术网

从两个javascript数组创建一个简单的测验

从两个javascript数组创建一个简单的测验,javascript,php,html,arrays,Javascript,Php,Html,Arrays,基本上我有两个数组:一个单词数组和一个定义数组。 单词数组由一组单词组成,这些单词由定义数组中的相应定义匹配:即单词[0]=定义[0],依此类推。 我希望实现的是,我想给用户一个测验,从单词数组中随机出现一个单词,用户必须在文本框中输入定义,这样当用户输入最后一个定义时,不会重复任何单词,也不会遗漏任何单词。我能够实现其中的一部分,以下是我丑陋的代码: var word = "<?php echo $word; ?>";//getting words from db to a j

基本上我有两个数组:一个单词数组和一个定义数组。 单词数组由一组单词组成,这些单词由定义数组中的相应定义匹配:即单词[0]=定义[0],依此类推。 我希望实现的是,我想给用户一个测验,从单词数组中随机出现一个单词,用户必须在文本框中输入定义,这样当用户输入最后一个定义时,不会重复任何单词,也不会遗漏任何单词。我能够实现其中的一部分,以下是我丑陋的代码:

  var word = "<?php echo $word; ?>";//getting words from db to a js array
  var def = "<?php echo $def; ?>";//same for definition
  var finalword = word.split(",");//final word array
  var finaldef = def.split(",");//final definition array
  function randomize() {
    document.getElementById("answer").value = "";
    document.getElementById("success").innerHTML = "";
    document.getElementById("fail").innerHTML = "";
    var random = finalword[Math.floor(Math.random()*finalword.length)];//randomize the word from array
    document.getElementById("question").innerHTML = random;
    for(var i=0;i<=finalword.length;i++) { //find the index of the random word and match it with the index of definition
    if(finalword[i]==random) {
      console.log(i);
      var randomdef = i;
      answerdef = finaldef[randomdef];
      console.log(answerdef);
    }
    }


  }

  function extract(a) {
   //check if indices are equal
    var answer = document.getElementById("answer").value;
    console.log(answer);
    if(answerdef == answer) {
    var right = document.getElementById("success");
    document.getElementById("fail").innerHTML = "";
    right.innerHTML = "Whoopie, correct answer, let's move onto the next question.";
    right.className = right.className + "animated infinite pulse";
    }
    else {
      var wrong = document.getElementById("fail");
      var input = document.getElementById("input");
      input.className = input.className + "animated infinite shake";
      wrong.innerHTML = "Oopsie, hold your horses. The answer is incorrect.";
      wrong.className = wrong.className + "animated infinite pulse";
    }
  } //ignore the css and other calls.
var-word=”“//将单词从db获取到js数组
var def=“”//定义相同
var finalword=单词分割(“,”)//最终字数组
var finaldef=定义拆分(“,”)//最终定义数组
函数随机化(){
document.getElementById(“答案”).value=“”;
document.getElementById(“成功”).innerHTML=“”;
document.getElementById(“失败”).innerHTML=“”;
var random=finalword[Math.floor(Math.random()*finalword.length)];//从数组中随机选择单词
document.getElementById(“问题”).innerHTML=random;

对于(var i=0;i如果我是你,我不会使用数组,但是因为你显然在学习,所以我将给你一个简单的例子,尽可能清楚地说明这一点

请随意运行代码段以查看其实际操作,并复制我添加的所有css和html。我没有使用任何单独的库,因为您没有专门针对任何库,但可以使用jQuery或下划线等简化代码

//定义我们将在代码中使用的变量
var words=[“猫”、“狗”、“老鼠”、“马”];
var defin=[“猫的定义”,
“狗的定义”,
“鼠标的定义”,
“马的定义”
];
var得分=0;
var合计=0;
var currentIndex=-1;
//第一个问题
nextQuestion();
//按一下按钮
document.getElementById('next')。onclick=function(){
if(document.getElementById(“输入”).value==“”){
//用户尚未输入任何文本
}否则{
if(document.getElementById(“输入”).value==defin[currentIndex]){
//正确答案
document.getElementById(“分数”).className=“好”;
分数++;
}否则{
//答错了
document.getElementById(“score”).className=“坏”;
}
//更新分数
document.getElementById(“score”).innerHTML=score;
document.getElementById(“total”).innerHTML=total;
//清除输入
document.getElementById(“输入”).value=“”;
nextQuestion();
}
};
函数nextQuestion(){
//下一个问题,更新答案索引
currentIndex=Math.floor(Math.random()*words.length);
document.getElementById(“问题”).innerHTML=words[currentIndex];
总计++;
}
。不好{
颜色:红色;
}
.好{
颜色:绿色;
}
得分:0分,共0分
下一步