gpt4 book ai didi

javascript - if 测验的条件

转载 作者:行者123 更新时间:2023-11-28 03:26:33 25 4
gpt4 key购买 nike

我正在做一个测验,根据给出的答案,应该返回一个字符串(向上、向下或中),我将其添加到组件的 className 中以显示正确的图标。

谁能告诉我这个逻辑我做错了什么?到目前为止,两个参数(answer 和 id)都变得很好,我可以阅读它们。但 if 语句只是忽略它们,并直接跳转到 else 语句(总是给出“down”)。

这是我写的逻辑:

export const checkAnswer = (answer, id) => {
if ((answer === 'Nein') && (id === '0' || id === '3' || id === '5')) {
return 'up';
} else if ((answer === 'Ja') && (id === '1' || id === '2' || id === '4' || id === '6')){
return 'up';
} else if (answer === 'Weiß nicht') {
return 'medium';
} else {
return 'down';
}

我感谢任何人的帮助。

谢谢你们的帮助,伙计们。这是我传递参数的代码:

questions.map(question => {
if (checkAnswer(question.answer, question.id) === 'up') {
correctAnswers += 1;
debugger
}
if (
(question.category === 'Effektivkosten' || 'Rentenfaktor') &&
question.answer === 'Nein'
) {
correctAnswers += 2;
}

if (question.winner && question.answer === 'Ja') {
winnerAnswer = true;
}
});

所有问题的结构如下:

{
id: 0,
category: 'Effektivkosten',
text: 'Sind die Effektivkosten höher als 1,5%?',
shortText: 'Über 1,5% p.A.',
answer: '',
options: [
{
id: 1,
name: 'Ja'
},
{
id: 2,
name: 'Nein'
},
{
id: 3,
name: 'Weiß nicht'
}
]
},

这里是我读取“字符串”并将其添加到 className 的表:

 <table>
<tbody>
{questions.map((question, index) => (
<tr key={question.id}>
<td className="name">
<strong>{question.category}:</strong>
</td>
<td className="shortName">{question.shortText}</td>
<td>
<i
className={`icon-thumb icon-thumb__${checkAnswer(
question.answer,
question.id
)}`}
/>
</td>
</tr>
))}
</tbody>
</table>

最佳答案

正如评论中所指出的,您使用严格比较来检查 id 是否为 '1''2''3' ,但你的 ID 实际上是数字(根据你发布的问题结构)。

通过严格比较,如果您的输入是数字,则必须将它们与数字而不是字符串进行比较。

那就是

export const checkAnswer = (answer, id) => {
if ((answer === 'Nein') && (id === 0 || id === 3 || id === 5)) {
return 'up';
} else if ((answer === 'Ja') && (id === 1 || id === 2 || id === 4 || id === 6)){
return 'up';
} else if (answer === 'Weiß nicht') {
return 'medium';
} else {
return 'down';
}

关于javascript - if 测验的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com