简体   繁体   English

在其他条件下切换语句,否则无法达到

[英]Switch statement in if else conditional, else not being reached

This is a shortened version of a conditional I've written to parse information from a vehicle VIN number. 这是我编写的用于解析车辆VIN编号信息的条件的简化版本。 If I pass in a VIN such as JA3XXXXXXXXXXXXXX it returns and object with properties of region:'Asia';, country:'Japan'; 如果我传入了诸如JA3XXXXXXXXXXXXXX之类的VIN,它会返回并反对具有以下区域的属性:“亚洲”;国家/地区:“日本”; and make:'Isuzu'; 并制作:“五十铃”; but if I pass it 2A5XXXXXXXXXXXXXX I would expect an object with properties set to region:'North America'; 但是如果我将其传递给2A5XXXXXXXXXXXXXXXXX,我会期望一个对象的属性设置为region:'North America'; , country:'Canada'; ,国家/地区:“加拿大”; and make:'Chrysler'; 并做出:“克莱斯勒”; instead I get an object with the region property set to 'Asia' and that is it. 取而代之的是,我得到了一个区域属性设置为“亚洲”的对象。 Here is a jsFiddle with the code shown below. 是一个jsFiddle,代码如下所示。

var vehicle = {},
nthDigit = function (stringifiedVin, i) {
    var nthChar = stringifiedVin.charAt(i);
    return nthChar;
},
parseVin = function () {
    var i = 0;
    for (i = 0; i < 16; i += 1) {
        if (i === 0) {
            if (nthDigit(stringifiedVin, 0) === 'J' || 'K' || 'L' || 'M' || 'N' || 'P' || 'R') {
                vehicle.region = 'Asia';
                switch (nthDigit(stringifiedVin, i)) {
                case 'J':
                    vehicle.country = 'Japan';
                    switch (nthDigit(stringifiedVin, 1)) {
                    case 'A':
                        if (nthDigit(stringifiedVin, 2) === 'A' || 'B' || 'C' || 'D' || 'E' || 'F' || 'G' || 'H' || 'J' || 'K' || 'L' || 'M' || 'N' || 'P' || 'R' || 'S' || 'T' || 'U' || 'V' || 'W' || 'X' || 'Y' || 'Z') {
                            vehicle.make = 'Isuzu';
                        } else if (nthDigit(stringifiedVin, 2) === '3' || '4' || '5' || '6' || '7') {
                            vehicle.make = 'Mitsubishi';
                        } else {
                            vehicle.make = 'Not Read';
                        }
                        break;
                   }         
                   break;
               }
           } else if (nthDigit(stringifiedVin, 0) === '1' || '2' || '3' || '4' || '5') {
               vehicle.region = 'North America';
               if (nthDigit(stringifiedVin, 0) === '2') {
                   vehicle.country = "Canada";
                   switch (nthDigit(stringifiedVin, 1)) {
                   case 'A':
                       if (nthDigit(stringifiedVin, 2) === '2' || '3' || '4' || '5' || '6' || '7' || '8') {
                           vehicle.make = 'Chrysler';
                       } else {
                           vehicle.make = 'Not Read';
                       }
                       break;
                   }
                   break;
               }
           }
           return vehicle; 
        }
   }
}

I don't thing the || 我不喜欢|| parts work the way you think they do. 零件以您认为的方式工作。 This will run the Alert() since '2' is not false: 因为'2'不是false,所以它将运行Alert():

if ('A' === '1' || '2') 
    alert('match')

I would use a switch or you will have to spell it out like: 我会使用一个开关,否则您将需要像下面这样拼写:

var nd = nthDigit(stringifiedVin, 0);
if (nd === 'J' || nd === 'K' ||  nd === 'L' ||  ...

A shorter one: 较短的一个:

 if("JKLMNP".indexOf(nthDigit(stringifiedVin, 0)) > -1){
        //....
 }

@JBrooks I think you are right about how the double pipe or operator was causing trouble in the conditional statement. @JBrooks我认为您对双管道或运算符如何在条件语句中造成麻烦是正确的。 I think it is due to the way the operator handles falsy values as shown in this SO post. 我认为这是由于操作员处理虚假值的方式所致,如 SO帖子中所示。 I ended up getting rid of the if statements entirely when I realized the JA3XXXXXXXXXXXXXX should have been returning Mitsubishi and not Isuzu. 当我意识到JA3XXXXXXXXXXXXXX应该返回三菱而不是五十铃时,我最终完全摆脱了if语句。 I found the solution here using @KennyTM answer on setting up multiple cases. 我找到了解决办法在这里使用@KennyTM答案上设置多个案件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM