请帮助我进行此练习! 我有一个API链接https://opentdb.com/api.php?amount=10,我必须从该API获取数据才能使用JavaScript进行简单的测验(是的,但我不知道怎么做,对我来说很困难)。问题是我确实使问题出现在页面上,但是10个问题的答案相同。我的意思是应该是10个问题,里面有10个不同的数组,答案不同。我被困在这里,不知道该怎么做。:(
$。getJSON(url,function(data){ var myQuestions = data.results;
for (question of myQuestions) {
var correctAnswer = question.correct_answer;
var totAnswers = question.incorrect_answers;
totAnswers.push(correctAnswer);
// console.log(totAnswers);
}
function buildQuiz() {
const output = [];
myQuestions.forEach((currentQuestion, questionNumber) => {
const answers = [];
for (value of totAnswers) {
console.log(value);
answers.push(
`<label>
<input type="radio" name="question${questionNumber}" value="${value}">
${value}
</label>`
);
}
output.push(
`<div class="slide">
<div class="question"> ${currentQuestion.question} </div>
<div class="answers"> ${answers.join("")} </div>
</div>`
);
});
quizContainer.innerHTML = output.join('');
}
这是现在的样子:
我认为问题出在哪里,我将循环用于totAnswers变量。但是我不明白为什么。 有人可以给我一些提示吗?
答案 0 :(得分:0)
我不明白您为什么要成立totAnswers
您想要实现的目标可以这样完成:
function buildQuiz() {
const output = myQuestions.map((currentQuestion, questionNumber) => (
`<div class="slide">
<div class="question">
${currentQuestion.question}
</div>
<div class="answers">
${currentQuestion.incorrect_answers.concat(currentQuestion.correct_answer).join('')}
</div>
</div>`))
quizContainer.innerHTML = output.join('');
};