2 つのオブジェクトの配列があります。ユーザーがボタンを押すと、特定のオブジェクト プロパティの次の値が表示されるようにしたいと考えています。
これが私の配列です:
var allQuestions = [{
question: "This is question number one",
choices: ["one", "two", "three", "four"],
correctAnswer: "two"
}, {
question: "This is question number two",
choices: ["dog", "cat", "bear", "lion"],
correctAnswer: "bear"
}];
ボタンが押されたときに、次の「質問」のインスタンスが表示されるようにしたいと思います。
質問を切り替えるための私の機能は次のとおりです。
function switchQuestion() {
var singleQuestion = 0;
if(singleQuestion >= allQuestions.length) {
singleQuestion == 0;
} else {
singleQuestion == ""; // not sure what should go here
}
document.getElementById('question').innerHTML = allQuestions[singleQuestion].question;
}