var q = [{
question1: "What is the capital of California?",
choices: ["LA", "SF", "Sac"],
correctAnswer:"Sacramento"},
{question2: "What is the capital of Arizona?",
choices: ["A", "B", "C"],
correctAnswer:"B"},
{question3: "What is the capital of Washington?",
choices: ["D", "E", "F"],
correctAnswer:"E"}];
我正在尝试制作一个有趣的测验应用程序,这就是我到目前为止所拥有的。我创建了这个数组,我希望能够在每次用户点击“提交”按钮时循环遍历它并将问题和选择打印为单选输入。
这就是我到目前为止所拥有的。我意识到现在我只打印 question1 但我不太确定如何从这里开始。
(function () {
function init() {
$('.submitBtn').hide();
generateQuestions();
}
function generateQuestions() {
var q = [{
question1: "What is the capital of California?",
choices: ["Los Angeles", "San Francisco", "Sacramento"],
correctAnswer: "Sacramento"
}, {
question2: "What is the capital of Arizon?",
choices: ["Los Angeles", "San Francisco", "Sacramento"],
correctAnswer: "Sacramento"
}, {
question3: "What is the capital of Washington?",
choices: ["Los Angeles", "San Francisco", "Sacramento"],
correctAnswer: "Sacramento"
}];
var quiz = $('.quiz');
$.each(q, function (index, obj) {
$.each(obj, function (key, value) {
$('.getStarted').click(function () {
$(this).fadeOut(500);
quiz.append(obj.question1);
$('.submitBtn').fadeIn(500);
});
});
});
}
init();
})();
问题:我如何正确地遍历这个数组并打印每个问题以及它的选择。你可以在上面看到我的尝试。