2
\$\begingroup\$

I have a switch statement that checks whether or not one string matched another that was stored in an array. However, there are multiple cases in which the same result is returned for multiple matches. I originally had a large amount of if/else checks, but tried to shorten that down with the switch statement. I was wondering, is there anyway to shorten down this switch statement?


function checkLoc(xstreets) {
            var address;
            if(xstreets[2] == undefined) {
                address = xstreets[0]+" & "+xstreets[1]+" Chicago IL";
            }
            else {
                switch(xstreets[2]) {
                    case "aon":
                        address = "Aon Center Chicago IL";
                        break;
                    case "trump":
                        address = "401 North Wabash Avenue, Chicago, IL";
                        break;
                    case "pritzker":
                        address = "Pritzker Park Chicago IL";
                        break;
                    case "hyde":
                    case "hydepark":
                    case "uofc":
                    case "uchicago":
                        address = "5801 South Ellis Avenue, Chicago, IL";
                        break;
                    case "reg":
                        address = "1100 E 57th St, Chicago, IL 60637";
                        break;
                    case "willis":
                    case "wttw":
                        address = "233 South Wacker Drive, Chicago, IL";
                        break;
                    case "600":
                        address = "600 West Chicago Avenue, Chicago, IL";
                        break;
                    default:
                        address = xstreets[0]+" & "+xstreets[1]+" Chicago IL";
                        break;
                }
            }
            return address;
        }
\$\endgroup\$
4
\$\begingroup\$

You can convert it to two array. The first will normalize multiple "aliases" of the same key into one and second will have your address values.

var aliases = {
  "hyde" : "hydepark",
  "uofc" : "hydepark",
  "uchicago" : "hydepark",
  "wttw" : "willis"
};

var addresses = {
  "hydepark" : "5801 South Ellis Avenue, Chicago, IL",
  ...
};

var alias = aliases[xstreets[2]];
if(alias == undefined) {
  alias = xstreets[2]
}
var address = addresses[alias];
if(address == undefined) {
  return xstreets[0]+" & "+xstreets[1]+" Chicago IL";
}
return address;
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Taking a step further, the function... I just turned into a one liner:

    //array indices are shorter than using aliases
    //we index streets to your addresses
var streets = {
        'aon': 0,
        'trump': 1,
        'pritzker': 2,
        'hyde': 3,
        'hydepark': 3,
        'uofc': 3,
        'uchicago': 3,
        'reg': 4,
        'willis': 5,
        'wttw': 5,
        '600': 6
    },
    addresses = [
          'Aon Center Chicago IL', 
          '401 North Wabash Avenue, Chicago, IL', 
          'Pritzker Park Chicago IL', 
          '5801 South Ellis Avenue, Chicago, IL', 
          '1100 E 57th St, Chicago, IL 60637',
          '233 South Wacker Drive, Chicago, IL',
          '600 West Chicago Avenue, Chicago, IL'
    ];

function checkLoc(xstreets) {

    //an undefined index/key returns undefined
    //we can use "||" which acts like loose comparison
    //if the value before "||" is falsy, it evaluates the value
    //after "||". If the value after "||" is not followed by another 
    //operator, it's considered the "default value"
    return addresses[streets[xstreets[2]]] || xstreets[0] + " & " + xstreets[1] + " Chicago IL";
}

//single check
console.log(checkLoc(['foo','bar','aon']));

//multi check
console.log(checkLoc(['foo','bar','uofc']));
console.log(checkLoc(['foo','bar','uchicago']));

//nonexistent street
console.log(checkLoc(['foo','bar','baz']));
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.