【发布时间】:2021-01-22 15:40:13
【问题描述】:
const questions = [
{
"question": "What is the scientific name of a butterfly?",
"answers": ["Apis", "Coleoptera", "Formicidae", "Rhopalocera"],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": ["1,233 K", "5,778 K", "12,130 K", "101,300 K"],
"correctIndex": 1
},
]
我正在尝试这种方式但返回 -1:
console.log(questions.findIndex(value => value.answers.indexOf() === 'Apis'));
例如,如果想得到 indexOf 'Apis',我得到 -1。
我正在尝试将“CorrectIndex”值与答案数组值的索引进行比较,并返回是否正确。
【问题讨论】:
-
questions是一个数组,它可能包含多个项目。如果有多个应该怎么办?在获得答案索引之前,我们是否知道使用什么索引来获得正确的问答项目? -
value.answers.indexOf()缺少参数。为什么要尝试将数字与字符串进行比较? -
您的问题缺乏上下文。
'Apis'来自哪里?您如何知道需要将其与questions中的哪个问题进行比较?
标签: javascript html indexof