我正在尝试添加至少两个可能的正确答案,而且似乎无法做到。下面是JavaScript的代码,如果需要我可以发布html。我正在尝试做什么?如果是这样,我该怎么办?
var questions = [];
questions.push({
q: "Rate your custard making skills from 1-5.",
a: "1",
b: "2",
c: "3",
answer: "b"
});
questions.push({
q: "what is question two?",
a: "answer one.",
b: "answer two.",
c: "answer three.",
answer: "b"
});
var i = 0;
var answers = [];
window.onload = function() {
var n = document.getElementById('next');
n.onclick = next;
loadNext();
};
function next() {
var aa = document.getElementById('aa');
var ab = document.getElementById('ab');
var ac = document.getElementById('ac');
var ans = null;
if (i == 0) loadNext();
else {
if (aa.checked) ans = "a";
if (ab.checked) ans = "b";
if (ac.checked) ans = "c";
if (ans) {
answers.push(ans);
loadNext();
} else alert("Please Select an answer.");
}
};
function loadNext() {
var qq = document.getElementById('qq');
var qa = document.getElementById('qa');
var qb = document.getElementById('qb');
var qc = document.getElementById('qc');
if (i >= questions.length) endQuiz();
else {
qq.innerHTML = questions[i].q;
qa.innerHTML = questions[i].a;
qb.innerHTML = questions[i].b;
qc.innerHTML = questions[i].c;
i++;
document.getElementById('aa').checked = false;
document.getElementById('ab').checked = false;
document.getElementById('ac').checked = false;
}
};
function endQuiz() {
var score = 0;
for (var j = 1; j < questions.length; j++)
if (answers[j] == questions[j].answer)
score++;
score *= 2;
alert("You Scored: " + score);
};
答案 0 :(得分:0)
您不能多次拥有相同的属性'b'。
将答案更改为数组:
questions.push({
q: "Rate your custard making skills from 1-5.",
a: "1",
b: "2",
c: "3",
answer: ["a", "b"],
scores: [1, 2]
});
更改next
函数以填充答案数组:
function next() {
var aa = document.getElementById('aa');
var ab = document.getElementById('ab');
var ac = document.getElementById('ac');
var ans = []; // <-- here
if (i == 0) loadNext();
else {
if (aa.checked) ans.push("a"); // <-- here
if (ab.checked) ans.push("b"); // <-- here
if (ac.checked) ans.push("c"); // <-- here
if (ans.length > 0) { // <-- here
answers.push(ans);
loadNext();
} else alert("Please Select an answer.");
}
};
更改验证功能以比较问题答案数组:
function endQuiz() {
var score = 0;
var correct;
for (var j = 1; j < questions.length; j++) {
correct = answers[j].length == questions[j].answer.length;
if(correct) {
for(var an=0;an<questions[j].length;an++) {
if (answers[j][an] != questions[j].answer[an]){
correct = false;
break;
}
}
}
if(correct) {
score++;
}
}
score *= 2;
alert("You Scored: " + score);
};
如果我理解正确,您需要分别为每个可能的选择设置分数。如果是这种情况,您可以执行以下操作:
在问题对象中添加scores
数组,该数组映射选项的分数,此处a = 1,b = 2:
questions.push({
q: "Rate your custard making skills from 1-5.",
a: "1",
b: "2",
c: "3",
answer: ["a", "b"],
scores: [1, 2]
});
更改验证功能以根据选项添加分数:
function endQuiz() {
var score = 0;
for (var j = 0; j < questions.length; j++) {
for(var an=0;an<questions[j].answer.length;an++) {
var ansIndex = answers[j].indexOf(questions[j].answer[an]);
if(ansIndex > -1) {
score += questions[j].scores[an];
}
}
}
score *= 2;
alert("You Scored: " + score);
};
演示:JSFiddle