使用JavaScript

时间:2016-03-16 20:04:25

标签: arrays object

我是新来的?我正在学习Javascript。今天我遇到了一些问题,希望你能帮我解决。

我试图在没有html,javascript的情况下进行测验。问题和答案我想从问题数据库导入,数据库有一个数组iwth对象的形式。

所以我做了标题和问题。问题是,如何让每个问题都有自己的答案变体。这是我的代码`

var QBase=[];

// Question 1
var q = {
    q:"Question 1",
    a:[
    "Answer 11",
    "Answer 12"
    ]
}
QBase.push(q);

// Question 2
var q = {
    q:"Question 2",
    a:[
    "Answer 21",
    "Answer 22",
    "Answer 23",
    "Answer 24",
    "Answer 25"
    ]
}

QBase.push(q);

// Question 3
var q = {
    q:"Question 3",
    a:[
    "Answer 31",
    "Answer 32",
    "Answer 33"
    ]
}
QBase.push(q);

var qForm = {

ancestor:document.body,
wrapper:'',
header:'',
questionBlock:'',
question:[],
answerBlock:[],
answer:[],


createForm:function(){
    this.wrapper=document.createElement('form');
    this.wrapper.classList.add('wrapper');
    this.ancestor.appendChild(this.wrapper);
},
createHeader:function(){
    this.header=document.createElement('h1');
    this.wrapper.classList.add('header');
    this.header.innerHTML='LALALALAAL';
    this.wrapper.appendChild(this.header);
},
createQuestionBlock:function(){
    this.questionBlock=document.createElement('ol');
    this.questionBlock.classList.add('questionBlock');
    this.wrapper.appendChild(this.questionBlock);
},
createQuestion:function(i){
    this.question[i]=document.createElement('li');
    this.question[i].classList.add('question'+i);   
    this.question[i].innerHTML=QBase[i].q;
    this.questionBlock.appendChild(this.question[i]);


},
createAnswerBlock:function(i){
    this.answerBlock[i]=document.createElement('ol');
    this.answerBlock[i].classList.add('AnswerBlock');
    this.question[i].appendChild(this.answerBlock[i]);
},
createAnswer:function(i){
    this.answer[i]=document.createElement('li');
    this.answer[i].classList.add('answer'+i);
    this.answer[i].innerHTML=QBase[i].a;
    this.answerBlock[i].appendChild(this.answer[i]);

},

createTest:function(){
    this.createForm();
    this.createHeader();
    this.createQuestionBlock();
    for (var i=0; i<QBase.length; i++){
        this.createQuestion(i);
    }
    for (var i=0;i<QBase.length;i++){
        this.createAnswerBlock(i);
    }
    for (var i=0;i<QBase.length;i++){
        for(var j=0;j<QBase[i].a.length;j++){
            this.createAnswer(a[i],q[j]);
        }
    }
}

} qForm.createTest(); ` 输出看起来像这个Output

但我需要这样的desirable output

1 个答案:

答案 0 :(得分:0)

尝试将答案嵌套在for循环中

类似的东西:

set /a calctotal += %%i %% 5 + 3