我想循环所有这些代码以进行3次不同的测验,这是可能的还是我必须重复所有这些代码两次?
每个测验应该有3个不同的问题,以及每个问题的3个不同答案。然后,每个测验中应该有不同的警报,这取决于用户正确回答了多少问题。
期望的结果: 3个不同的测验,使用相同的代码来浏览每个问题,所有测试都显示不同的结束警报。
i@scheherezade:/opt/plyr/src$ file *o
loop_apply.o: LLVM IR bitcode
RcppExports.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
split-numeric.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

var iconquiz = [{
"question": "which icon is used for GitHub",
"answers": ["<i class='fa fa-gitlab fa-lg' aria-hidden='true'></i>", "<i class='fa fa-github-alt fa-lg' aria-hidden='true'></i>", "<i class='fa fa-grav fa-lg' aria-hidden='true'></i>"],
"correct": 2,
"was_correct": 0
},
{
"question": "What icon is this?<i class='fa fa-gitlab fa-lg' aria-hidden='true'></i>",
"answers": ["GitHub", "Google", "GitLab"],
"correct": 3,
"was_correct": 0
},
{
"question": "which icone is used by PayPal?",
"answers": ["<i class='fa fa-pinterest-square' aria-hidden='true'></i>", "<i class='fa fa-pied-piper-pp' aria-hidden='true'></i>", "<i class='fa fa-paypal' aria-hidden='true'></i>"],
"correct": 3,
"was_correct": 0
}
];
//console.log(quiz[0].answers);
var index = -1;
var correct = 0;
function nextButton() {
if (index < iconquiz.length - 1) {
index++;
document.getElementById("questionHere").innerHTML = iconquiz[index].question;
document.getElementById("answer1.0").innerHTML = "<input class='answers' type='radio' name='answer' data-question=" + index + " value='1' >" + iconquiz[index].answers[0];
document.getElementById("answer2.0").innerHTML = "<input class='answers' type='radio' name='answer' data-question=" + index + " value='2' >" + iconquiz[index].answers[1];
document.getElementById("answer3.0").innerHTML = "<input class='answers' type='radio' value='3' data-question=" + index + " name='answer'>" + iconquiz[index].answers[2];
var radios = document.getElementsByClassName("answers");
for (radio in radios) {
radios[radio].onchange = function() {
if (iconquiz[this.dataset.question].was_correct == 0) {
if (iconquiz[this.dataset.question].correct == this.value) {
correct++;
iconquiz[this.dataset.question].was_correct = 1;
}
} else if (iconquiz[this.dataset.question].correct != this.value) {
correct--;
iconquiz[this.dataset.question].was_correct = 0;
}
console.log(correct);
}
}
} else {
alert("Correct answers :" + correct);
}
}
function prevButton() {
if (index > 0) {
index--;
document.getElementById("questionHere").innerHTML = iconquiz[index].question;
document.getElementById("answer1.0").innerHTML = "<input class='answers' type='radio' name='answer' data-question=" + index + " value='1' >" + iconquiz[index].answers[0];
document.getElementById("answer2.0").innerHTML = "<input class='answers' type='radio' name='answer' data-question=" + index + " value='2' >" + iconquiz[index].answers[1];
document.getElementById("answer3.0").innerHTML = "<input class='answers' type='radio' name='answer' data-question=" + index + " value='3' >" + iconquiz[index].answers[2];
}
}
&#13;
/*************
MAIN STUFFF
*************/
body {
background: #CCC;
font-family: 'Varela Round', sans-serif;
}
.collapse {
color: #fff;
background: #494949;
border-radius: 10px;
width: 300px;
margin: 20px auto;
padding: 10px 0;
box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}
/*************
QUIZ BOXES
*************/
h2 {
text-align: center;
}
input {
display: none;
visibility: hidden;
}
.answers {
display: inline;
visibility: visible;
}
label {
width: 90px;
margin: 0 auto;
margin-bottom: 10px;
display: block;
text-align: center;
color: #fff;
background-color: #4ada95;
border-radius: 5px;
}
label:hover {
color: #494949;
cursor: pointer;
}
label::before {}
/************
QUIZ CONTENT
************/
h3 {
background-color: #707070;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
margin: 0;
padding: 10px;
}
li {
list-style-type: none;
padding: 10px;
margin: 0 auto;
text-align: center;
}
button {
color: #fff;
background-color: #707070;
border-radius: 5px;
border-style: solid;
border-color: #707070;
margin: 5px;
}
.buttons {
position: absolute;
bottom: 0;
text-align: center;
margin-left: 73px;
}
/***********
QUIZES
***********/
#expand,
#expand2,
#expand3 {
height: 225px;
overflow: hidden;
transition: height 0.5s;
background-color: #4ada95;
color: #FFF;
width: 250px;
margin: 0 auto;
text-align: center;
border-radius: 10px;
position: relative;
}
/**********
FIRST QUIZ
**********/
#toggle:checked~#expand {
height: 0px;
}
#toggle:checked~label::before {
display: hidden;
}
/**********
SECOND QUIZ
**********/
#toggle2:checked~#expand2 {
height: 0px;
}
#toggle2:checked~label::before {
display: hidden;
}
/**********
THIRD QUIZ
**********/
#toggle3:checked~#expand3 {
height: 0px;
}
#toggle3:checked~label::before {
display: hidden;
}
&#13;