亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查無線電輸入時我的 if 語句不起作用

檢查無線電輸入時我的 if 語句不起作用

開心每一天1111 2023-07-29 13:09:30
我正在嘗試驗證無線電輸入以檢查其答案是否正確。但它跳過了線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();
查看完整描述

3 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

我認為你的 If 語句應該看起來像這樣

if(answer == allQuestions[q].choices[allQuestions[q].correctAnswer]) {
        alert("correct");
    }

編輯了這個,剛剛嘗試并為我工作


查看完整回答
反對 回復 2023-07-29
?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您還可以使用數組中元素的索引進行比較。


if(allQuestions[q].choices.indexOf(answer) == allQuestions[q].correctAnswer) {

    alert("correct");

}


查看完整回答
反對 回復 2023-07-29
?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

因為我做到了...


const allQuestions = 

        [ { question     : 'Where is Helsinki?'

          , choices      : [ 'Sweden', 'Finland', 'Us' ] 

          , correctAnswer: 1

          } 

        , { question     : 'Where is Stockholm?'

          , choices      : [ 'Norway', 'Iceland', 'Sweden' ] 

          , correctAnswer: 2

          } 

        , { question     : 'Where is K?penhamn?'

          , choices      : [ 'Denmark', 'Sweden', 'Norway' ] 

          , correctAnswer: 0

          } 

        ] 

function* qList(arr) { for (let q of arr) yield q }


const libQuestion  = document.querySelector('h1')

  ,   formReplies  = document.getElementById('list')

  ,   btNext       = document.querySelector('button')

  ,   DomParser    = new DOMParser()

  ,   qListRead    = qList( allQuestions )

  ,   score        = { correct: 0, count:0 }

  ;

var currentAnswer = ''

  ;

function setNewQuestion()

  {

  formReplies.innerHTML = ''


  let newQuestion = qListRead.next()

  if (!newQuestion.done)

    {

    libQuestion.textContent = newQuestion.value.question

    currentAnswer           = newQuestion.value.correctAnswer

    ++score.count

    newQuestion.value.choices.forEach((choice,indx)=>

      {

      let line = `<label><input type="radio" name="answer" value="${indx}">${ choice}</label>`

        , inLn = (DomParser.parseFromString( line, 'text/html')).body.firstChild

        ;

      formReplies.appendChild(inLn)

      })

    }

  else

    {

    libQuestion.textContent = ` Score = ${score.correct} / ${score.count}`

    btNext.disabled         = true

    }

  }

setNewQuestion()

btNext.onclick=()=>

  {

  if (formReplies.answer.value)

    {

    score.correct += ( currentAnswer == formReplies.answer.value )

    setNewQuestion()

    }

  }

<h1></h1>

<p></p>

<form id="list"></form>

<br>

<button>next</button>


查看完整回答
反對 回復 2023-07-29
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號