简体   繁体   中英

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. If I pass in a VIN such as JA3XXXXXXXXXXXXXX it returns and object with properties of region:'Asia';, country:'Japan'; and make:'Isuzu'; but if I pass it 2A5XXXXXXXXXXXXXX I would expect an object with properties set to 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.

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:

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. I think it is due to the way the operator handles falsy values as shown in this SO post. I ended up getting rid of the if statements entirely when I realized the JA3XXXXXXXXXXXXXX should have been returning Mitsubishi and not Isuzu. I found the solution here using @KennyTM answer on setting up multiple cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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