我正在使用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,因此评估为true。您需要分别进行比较,或者检查值是否在包含“灰色”,“蓝色”和“黑色”的数组中

09-11 17:46