简体   繁体   English

javascript我怎么解决这个问题,请帮帮我

[英]How can i solve this problem in javascript, please help me

So im making a quiz app, im fetching data from a quiz api, the object of every quiz api looks like this:所以我正在制作一个测验应用程序,我从测验 api 中获取数据,每个测验 api 的 object 看起来像这样:

{
   "correctAnswer": "Cycling",
   "incorrectAnswers": [
     "American Football",
     "Cricket",
     "Tennis"
   ],
   "question": "What sport would you associate with The Tour de France?",
}

As you can see, its an object that has a correct answer and an array with the incorrect answers.如您所见,它是一个具有正确答案的 object 和一个具有错误答案的数组。 I have made two constants, one with the value of the correct answer and the other one is an array with the incorrect answers.我做了两个常量,一个是正确答案的值,另一个是一个包含错误答案的数组。 I then sorted the two arrays (so they are mixed and the correct answer isn't always in the same place) and then i put the questions into the innerhtml of a container like this:然后我对两个 arrays 进行了排序(因此它们是混合的并且正确答案并不总是在同一个地方)然后我将问题放入容器的 innerhtml 中,如下所示:

const correctQuestions = data[num].correctAnswer;
const incorrectQuestions = data[num].incorrectAnswers;
const combinedQuestions = [correctQuestions, ...incorrectQuestions].sort()
console.log([correctQuestions, ...incorrectQuestions])


      answerContainer.innerHTML = combinedQuestions.map((item,index) => 
` 
    <li><input type="radio" value = /*TRUE OR FALSE!!!, HOW DO I INCLUDE IT HERE?*/id=${index} name=${index}/> <label for=${index}>${item}</label></li>

    `

      ).join('')

But i have a very big problem, i already solved one, which is to sort the array so the correct answer is mixed with the incorrect ones.但是我有一个非常大的问题,我已经解决了一个问题,那就是对数组进行排序,以便将正确答案与错误答案混合在一起。 But the other problem is, each input doesn't include their corresponding values, if the answer is true then its value should be true, but i have no idea how to include the value into the input, always maintaining the array mixed.但另一个问题是,每个输入不包含它们对应的值,如果答案为真那么它的值应该为真,但我不知道如何将值包含到输入中,始终保持数组混合。

So in summary the question is: How can i put the value of true or false in each input?所以总而言之,问题是:我如何在每个输入中输入 true 或 false 的值? Or is there any other way of displaying the answers but with the true or false value?或者是否有任何其他方式显示答案但具有 true 或 false 值?

Test whether item == correctQuestions and put the appropriate value there.测试是否item == correctQuestions并将适当的值放在那里。

answerContainer.innerHTML = combinedQuestions.map((item,index) => 
` 
    <li><input type="radio" value="${item == correctQuestions ? "true" : "false"}" id="${index}" name="answers" /> <label for="${index}">${item}</label></li>

    `
      ).join('')

Also you should not use name=${index} .你也不应该使用name=${index} A radio button group is identified by them having the same name.单选按钮组由具有相同名称的它们标识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM