检查无线电输入时我的 if 语句不起作用

我正在尝试验证无线电输入以检查其答案是否正确。
但它跳过了线if (answer == allQuestions[q].correctAnswer)

这是完整代码https://jsfiddle.net/alcatel/sopfmevh/1/

for (let k = 0; k < answers.length; k++) {

  if (answers[k].checked) {

    answer = answers[k].value;

  }

}


// IT SKIPS THIS LINE !!

if (answer == allQuestions[q].correctAnswer) { // IT SKIPS THIS LINE!!

  alert("correct");

}


q++;

    

populate();


开心每一天1111
浏览 128回答 3
3回答

眼眸繁星

我认为你的 If 语句应该看起来像这样if(answer&nbsp;==&nbsp;allQuestions[q].choices[allQuestions[q].correctAnswer])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("correct"); &nbsp;&nbsp;&nbsp;&nbsp;}编辑了这个,刚刚尝试并为我工作

jeck猫

您还可以使用数组中元素的索引进行比较。if(allQuestions[q].choices.indexOf(answer) == allQuestions[q].correctAnswer) {&nbsp; &nbsp; alert("correct");}

动漫人物

因为我做到了...const allQuestions =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [ { question&nbsp; &nbsp; &nbsp;: 'Where is Helsinki?'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , choices&nbsp; &nbsp; &nbsp; : [ 'Sweden', 'Finland', 'Us' ]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , correctAnswer: 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; , { question&nbsp; &nbsp; &nbsp;: 'Where is Stockholm?'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , choices&nbsp; &nbsp; &nbsp; : [ 'Norway', 'Iceland', 'Sweden' ]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , correctAnswer: 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; , { question&nbsp; &nbsp; &nbsp;: 'Where is Köpenhamn?'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , choices&nbsp; &nbsp; &nbsp; : [ 'Denmark', 'Sweden', 'Norway' ]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , correctAnswer: 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp;function* qList(arr) { for (let q of arr) yield q }const libQuestion&nbsp; = document.querySelector('h1')&nbsp; ,&nbsp; &nbsp;formReplies&nbsp; = document.getElementById('list')&nbsp; ,&nbsp; &nbsp;btNext&nbsp; &nbsp; &nbsp; &nbsp;= document.querySelector('button')&nbsp; ,&nbsp; &nbsp;DomParser&nbsp; &nbsp; = new DOMParser()&nbsp; ,&nbsp; &nbsp;qListRead&nbsp; &nbsp; = qList( allQuestions )&nbsp; ,&nbsp; &nbsp;score&nbsp; &nbsp; &nbsp; &nbsp; = { correct: 0, count:0 }&nbsp; ;var currentAnswer = ''&nbsp; ;function setNewQuestion()&nbsp; {&nbsp; formReplies.innerHTML = ''&nbsp; let newQuestion = qListRead.next()&nbsp; if (!newQuestion.done)&nbsp; &nbsp; {&nbsp; &nbsp; libQuestion.textContent = newQuestion.value.question&nbsp; &nbsp; currentAnswer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= newQuestion.value.correctAnswer&nbsp; &nbsp; ++score.count&nbsp; &nbsp; newQuestion.value.choices.forEach((choice,indx)=>&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; let line = `<label><input type="radio" name="answer" value="${indx}">${ choice}</label>`&nbsp; &nbsp; &nbsp; &nbsp; , inLn = (DomParser.parseFromString( line, 'text/html')).body.firstChild&nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; formReplies.appendChild(inLn)&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; libQuestion.textContent = ` Score = ${score.correct} / ${score.count}`&nbsp; &nbsp; btNext.disabled&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= true&nbsp; &nbsp; }&nbsp; }setNewQuestion()btNext.onclick=()=>&nbsp; {&nbsp; if (formReplies.answer.value)&nbsp; &nbsp; {&nbsp; &nbsp; score.correct += ( currentAnswer == formReplies.answer.value )&nbsp; &nbsp; setNewQuestion()&nbsp; &nbsp; }&nbsp; }<h1></h1><p></p><form id="list"></form><br><button>next</button>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript