Javascript最后if语句总是读取
javascript last if statement always read
我使用angularjs作为框架来编写一个小测试。我写了4个if
表述。但由于某些无法解释的原因,即使没有选择任何条件/答案,也总是读取最后一个条件。
$scope.makeup
总是显示You should have makeup Four
,为什么?
如果if
语句中还没有满足任何条件,难道不应该忽略它吗?
var silverMetal = ($scope.userChoices.metal === 'silver');
var goldMetal = ($scope.userChoices.metal === 'gold');
var greenTone = ($scope.userChoices.tone === 'green');
var blueTone = ($scope.userChoices.tone === 'blue');
var greyBlueBlackEye = ($scope.userChoices.eye === 'grey' || 'blue' || 'black');
var greenHazelBrownEye = ($scope.userChoices.eye === 'green' || 'hazel' || 'brown');
var lightblondeGingerDarkblondeHair = ($scope.userChoices.hair === 'light blonde' || 'ginger' || 'dark blonde');
var lightbrownDarkbrownBlackHair = ($scope.userChoices.hair === 'light brown' || 'dark brown' || 'black');
if (silverMetal && greenTone && greyBlueBlackEye && lightblondeGingerDarkblondeHair) {
$scope.makeup = 'You should have makeup One';
}
if (silverMetal && greenTone && greyBlueBlackEye && lightbrownDarkbrownBlackHair) {
$scope.makeup = 'You should have makeup Two';
}
if (silverMetal && greenTone && greenHazelBrownEye && lightblondeGingerDarkblondeHair) {
$scope.makeup = 'You should have makeup Three';
}
if (silverMetal && greenTone && greenHazelBrownEye && lightbrownDarkbrownBlackHair) {
$scope.makeup = 'You should have makeup Four';
}
问题是这样的行:
var greyBlueBlackEye = ($scope.userChoices.eye === 'grey' || 'blue' || 'black');
像'blue'这样的字符串是真值,因此它被求值为($scope.userChoices.eye === 'grey') || true
,因此求值为真。您需要单独进行比较,或者检查该值是否在包含'grey', 'blue'和'black'的数组中
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 正在读取多个JSON对象's文本作为每个jQuery语句
- 从公共数组读取时,javascript if语句不能与ifValid一起使用
- 为什么javascript if else语句只寄存器读取最后一个else语句
- JS是如何读取if语句和遍历数组元素的
- php if语句中没有读取的变量
- 转换要在Ajax语句中读取的coldfusion变量
- Javascript最后if语句总是读取
- 浏览器是如何读取if/else语句的?
- 什么叫“使用xxx”?语句,以及如何读取该值
- 为什么if (false)语句中的代码被读取
- 不能读取属性'radCCType'在使用单选按钮创建if语句时未定义