简体   繁体   中英

Comparing a value against an array

I am attempting to compare value against my array, however, it is not working as it should. When the value is Ontario it should be returned to be abbreviated as 'ON' and when the value is not found in the array list then it should be returned the same value as it was put it. The first alert box returns 'Ontario'? What am I doing wrong here:

function shorten(value) {

    var prov_states = [
        ['alberta','AB'],
        ['british Columbia','BC'],
        ['manitova','MB'],
        ['new brunswick','NB'],
        ['newfoundland and labrador','NL'],
        ['nova scotia','NS'],
        ['northwest territories','NT'],
        ['new brunswick','NB'],
        ['nunavut','NU'],
        ['ontario','ON'],
        ['prince edward island','PE'],
        ['quebec','QC'],
        ['saskatchewan','SK'],
        ['yukon','YT'],
        ['alabama','AL'],    
        ['alaska','AK'],
        ['arizona','AZ'],
        ['arkansas','AR'],
        ['california','CA'],
        ['colorado','CO'],
        ['connecticut','CT'],
        ['delaware','DE'],
        ['florida','FL'],
        ['georgia','GA'],
        ['hawaii','HI'],
        ['idaho','ID'],
        ['illinois','IL'],
        ['indiana','IN'],
        ['iowa','IA'],
        ['kansas','KS'],
        ['kentucky','KY'],
        ['louisiana','LA'],
        ['maine','ME'],
        ['maryland','MD'],
        ['massachusetts','MA'],
        ['michigan','MI'],
        ['minnesota','MN'],
        ['mississippi','MS'],       
        ['missouri','MO'],
        ['montana','MT'],    
        ['nebraska','NE'],
        ['nevada','NV'],
        ['new hampshire','NH'],
        ['new jersey','NJ'],
        ['new mexico','NM'],
        ['new york','NY'],
        ['north carolina','NC'],
        ['north dakota','ND'],
        ['ohio','OH'],
        ['oklahoma','OK'],
        ['oregon','OR'],
        ['pennsylvania','PA'],
        ['rhode island','RI'],
        ['south carolina','SC'],
        ['south dakota','SD'],
        ['tennessee','TN'],
        ['texas','TX'],
        ['utah','UT'],
        ['vermont','VT'],
        ['virginia','VA'],      
        ['washington','WA'],
        ['west virginia','WV'],
        ['wisconsin','WI'],
        ['wyoming','WY']            
        ]


  for (var i=0, iLen=prov_states.length; i<iLen; i++) {

    if (value.toLowerCase() == prov_states[i][0]) { return prov_states[i][1]; }

    else { return value }

  }
}


function test() {

alert(shorten('Ontario'))

alert(shorten('DC'))


}

You probably want to use a map/associative array instead of what you have now; it's much easier to do it this way.

var prov_states = {
    "alberta": "AB",
    "british columbia": "BC",
    ...
}

Then you can just do:

return prov_states[value.toLowerCase()];

Your particular problem is because of what you're doing when you don't find a value:

else { return value }

This means that if the first match is not successful, you immediately return value . Instead you should only do that if you were unable to find a match at all.

A while loop should work:

var i = 0;
var state = null;
while(state === null && i < prov_states.length) {
    if(value.toLowerCase() === prov_states[i][0]) {
        state = prov_states[i][1];
    }

    i++;
}

return state === null ? value : state; //or just return state if you are okay with null

Another issue I see is that you have BC as "british Columbia" , which will never match because you are calling value.toLowerCase() , which lowercases the entire string.

your problem is with this

else { return value }

It's going to return the first time it checks a state and it doesn't match.

The simplest change you can do it the following

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
    if (value.toLowerCase() == prov_states[i][0]) { 
        return prov_states[i][1]; 
    }    
}
return null;//or whatever you want to return when it is not found.

An even better implementation would be this I know that I was ninja'd by Vivin Paliath on this one

var prov_states = {
    'alberta': 'AB',
    'british Columbia': 'BC'
};

...

alert(prov_states['Ontario'.toLowerCase()]);

Put the return statement of else outside of the loop. Remove the else.

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
  if (value.toLowerCase() == prov_states[i][0]) return prov_states[i][1];
}
return value;

Move the return value statement out of the for loop (and the else ). Your code is currently returning the supplied value if the first item in the loop doesn't match.

for (var i=0, iLen=prov_states.length; i<iLen; i++)
{
    if (value.toLowerCase() == prov_states[i][0])
    {
        return prov_states[i][1];
    }
}
return value;

The first value of your array is not equal to Ontario . Therefore, the first time your loop runs, your else condition is called, returning value . You should make the loop look like this:

for (var i=0, iLen=prov_states.length; i<iLen; i++) {
   if (value.toLowerCase() == prov_states[i][0]) { return prov_states[i][1]; }
}

return value;

Put your return statement AFTER the whole loop.

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