我正在学习使用HTML
,CSS
和Javascript
构建网页。我计划使用该页面显示3个随机问题,并选择3个答案,其中之一就是正确答案。在15个问题中选择3个随机问题。我已经有以下脚本,但是我希望每个随机问题都显示为每个问题分配3个答案。
var display = document.getElementById("questions");
var questions = ['What color is the sky?',
'What sound does a cow make?',
'How many stars are on the US flag?',
'How mad is Max?',
'Is this another question?'];
var questionTracker = [];
var questionAmount = 3;
// Iterate however many times
for (var i = 0; i < questionAmount; i++) {
// Keep creating random numbers until the number is unique
do {
var randomQuestion = Math.floor(Math.random() * questions.length);
} while (existingQuestions());
display.innerHTML += questions[randomQuestion] + '<br>';
// Add the question to the tracker
questionTracker.push(randomQuestion);
}
// If the current random number already exists in the tracker, return true
function existingQuestions() {
for (var i = 0; i < questionTracker.length; i++) {
if (questionTracker[i] === randomQuestion) {
return true;
}
}
return false;
}