随机答案的多项选择考试:JavaScript

时间:2018-01-16 13:18:43

标签: javascript arrays multidimensional-array random 2d

我正在尝试进行有多个"游泳池的考试。我需要随机抽取一个特定数字并与其他池一起洗牌的问题。我已经接近了。我尝试了两种随机拼接和随机值的方法。当我使用拼接时,它似乎压扁了我的2D阵列,问题和答案。随机值给了我重复的项目。

这个似乎"扁平阵列"并将问题和答案作为同一项目返回。

Array.prototype.randsplice = function() {
  var ri = Math.floor(Math.random() * this.length);
  //takes it out of the array
  var rs = this.splice(ri, 1);
  return rs
}
     

这个正确地重新运行数组,但是给了我dupilcate项目。

Array.prototype.randval = function() {
  var ri = Math.floor(Math.random() * this.length);
  var val = this[ri];
  return val;
}

Array.prototype.shuffle = function(){
    var i = this.length, j, temp;
    while(--i > 0){
        j = Math.floor(Math.random() * (i+1));
        temp = this[j];
        this[j] = this[i];
        this[i] = temp;
    }
    return this;
}

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

问题阵列

var pos = 0,
    test, test_status, question, choice, choices, chA, chB, chC,
    correct = 0;
var questions = [
    ["What is 10 + 4?", "12", "14", "16", "B"],
    ["What is 20 - 9?", "7", "13", "11", "C"],
    ["What is 7 x 3?", "21", "24", "25", "A"],
    ["What is 8 / 2?", "10", "10", "4", "C"]
];
var questions2 = [
    ["What is A?", "A", "B", "C", "A"],
    ["What is B?", "A", "B", "C", "B"],
    ["What is C?", "A", "B", "C", "C"],
    ["What is D?", "B", "C", "D", "C"]
];

组合阵列

var newArray = [];
var newArray2 = [];

function myFunction() {
    newArray.push(result);
    newArray2.push(result2);
    return newArray;
    return newArray2;
}

每个游泳池需要使用不同的数字

var pool1NumUsed = 2;
for (i = 0; i < pool1NumUsed; i++) {
  //alert("For loop 1"/*questions.length*/);
  var result = questions.randsplice();
  var result2 = questions2.randsplice();
  myFunction();
}
var resultCleaned = result.clean();
var combinedArrays = newArray.concat(newArray2).clean();
var combinedArraysRand = combinedArrays.shuffle();

我需要能够从每个游泳池中提取特定数量的问题,我将在决赛中拥有更多的2个游泳池,然后将它们一起洗牌。 在最终产品中,我还需要改变答案/干扰者(需要能够根据问题打开和关闭此功能。带有数字答案的问题需要按照从最低到最高的顺序排列。),构建补救措施页面,并获得一个分数。 非常感谢任何帮助。

Full Code

0 个答案:

没有答案