javascript - 如何在条件中使用 javascript 函数参数(字符串)
问题描述
这里是 js 的新手......出于必要使用一点:-)。
当参数(x)将“notionalLarge”写入控制台时,为什么我有条件地向控制台写入“shares”?
console.log(x);
if(x == 'sharesLarge' || 'sharesSmall' ) {
console.log("shares");
} else if (x == 'notionalLarge' || 'notionalSmall' ) {
console.log("notional");
} else {
console.log("error");
}
}```
CONSOLE:
[Log] notionalLarge (sharesOrNotional.js, line 2)
[Log] shares (sharesOrNotional.js, line 4)
解决方案
你的 if 条件是错误的。
if 条件将分为两部分:
X == 'sharesLarge'
这是错误的
'小股'
这等于
'SharesSmall' != null
这是真的。所以你的第一个 if 条件为真。
你想要达到的是
If(x == 'sharesLarge' || x == 'sharesSmall')
推荐阅读
- google-sheets - 谷歌根据用户选择的标签查询动态数据范围
- javascript - 如何获取长度以了解生成的数字数量?
- c - 如何在 C 中使用带有数组的随机数生成器
- python - 有没有一种快速的方法来遍历涉及计算的熊猫数据框?
- python - 如何从python中的N个不同正态分布中采样M次?在处理时间方面是否有“更快”的方式?
- julia - 声明任意数量的变量以创建多项式环 julia
- javascript - 将“确认”弹出按钮标签“确定”和“取消”更改为“继续”和“返回页面”
- oracle - Oracle - JSON_VALUE 返回 NULL
- java - 虽然循环错误重复我的用户输入提示
- android - Firebase Cloud Messaging 推送通知在 Android 上不能在前台工作,只能在后台工作