Post

4 followers Follow
2
Avatar

Using a contains() function in conjunction with a switch case

I'm trying to figure out the right way to use a contains() function in conjunction with a switch case:

function searchZone(state) {
return #FML#.contains(state);
};

switch (@Pick City, State)
{

case searchZone("CT", "ME", "MA", "NH", "NJ", "RI", "VT"): "Z0"; break;
case "DE", "NY", "PA": "Z1"; break;
case "MD", "NC", "SC", "VA", "DC", "WV": "Z2"; break;
case "AL", "FL", "GA", "MS", "TN": "Z3"; break;
case "IN", "KY", "MI", "OH": "Z4"; break;
case "IA", "MN", "MT", "ND", "SD", "WI": "Z5"; break;
case "IL", "KS", "MO", "NE": "Z6"; break;
case "AR", "LA", "OK", "TX": "Z7"; break;
case "AZ", "CO", "ID", "NV", "NM", "UT", "WY": "Z8"; break;
case "CA", "OR", "WA", "AK": "Z9"; break;
}

#FML# is what I need to figure out. Thank you!

Mitchell Ogden

Please sign in to leave a comment.

17 comments

0
Avatar

Hey @Rainer ,

I'm thinking I would like to use a key value array to accomplish this. I would like to check a location field for what the state is so I may detect the assigned zone the state is within. The zones never change. So what I'm going to try is if the location field contains the value then return the key. So it should be as simple as using a for loop with the indexOf function checking for the value. Wish me luck. iIf you have any tips let me know. Otherwise, I'll comment on here with my solution.

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

@Rainer .... What did I do wrong here?

var states = {
"Z0": "CT",
"Z0": "ME",
"Z0": "MA",
"Z0": "NH",
"Z0": "NJ",
"Z0": "RI",
"Z0": "VT",
"Z1": "DE",
"Z1": "NY",
"Z1": "PA",
"Z2": "MD",
"Z2": "NC",
"Z2": "SC",
"Z2": "VA",
"Z2": "DC",
"Z2": "WV",
"Z3": "AL",
"Z3": "FL",
"Z3": "GA",
"Z3": "MS",
"Z3": "TN",
"Z4": "IN",
"Z4": "KY",
"Z4": "MI",
"Z4": "OH",
"Z5": "IA",
"Z5": "MN",
"Z5": "MT",
"Z5": "ND",
"Z5": "SD",
"Z5": "WI",
"Z6": "IL",
"Z6": "KS",
"Z6": "MO",
"Z6": "NE",
"Z7": "AR",
"Z7": "LA",
"Z7": "OK",
"Z7": "TX",
"Z8": "AZ",
"Z8": "CO",
"Z8": "ID",
"Z8": "NV",
"Z8": "NM",
"Z8": "UT",
"Z8": "WY",
"Z9": "CA",
"Z9": "OR",
"Z9": "WA",
"Z9": "AK"
};
var location = @Pick City, State + "";
zones = Object.zones(states);
zone = "";
for(i = 0; i < zones.length; i++){

if(location.indexOf(states[i])=='-1'){
break;

}

else {
zone = zones[i]
}

}

zone + ""

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

@Rainer

This didn't work either...

var states = {
"Z0": "CT",
"Z0": "ME",
"Z0": "MA",
"Z0": "NH",
"Z0": "NJ",
"Z0": "RI",
"Z0": "VT",
"Z1": "DE",
"Z1": "NY",
"Z1": "PA",
"Z2": "MD",
"Z2": "NC",
"Z2": "SC",
"Z2": "VA",
"Z2": "DC",
"Z2": "WV",
"Z3": "AL",
"Z3": "FL",
"Z3": "GA",
"Z3": "MS",
"Z3": "TN",
"Z4": "IN",
"Z4": "KY",
"Z4": "MI",
"Z4": "OH",
"Z5": "IA",
"Z5": "MN",
"Z5": "MT",
"Z5": "ND",
"Z5": "SD",
"Z5": "WI",
"Z6": "IL",
"Z6": "KS",
"Z6": "MO",
"Z6": "NE",
"Z7": "AR",
"Z7": "LA",
"Z7": "OK",
"Z7": "TX",
"Z8": "AZ",
"Z8": "CO",
"Z8": "ID",
"Z8": "NV",
"Z8": "NM",
"Z8": "UT",
"Z8": "WY",
"Z9": "CA",
"Z9": "OR",
"Z9": "WA",
"Z9": "AK"
};
var location = @Pick City, State + "";
var zone = "";
for(i = 0; i < states.length; i++){
if(location.indexOf(states[i])==-1){
break;
}
else{
zone = states[i].key;
}
}

zone

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

Hi Mitchell,

you can do it this way:

var state = @pick city, state // or whereever you get state from; you can also get it with .substr from the address
var Z0 = "CT, ME, MA, NH, NJ, RI, VT";
var Z1 =  "DE, NY, PA" ;
var Z2 = "MD, NC, SC, VA, DC, WV";
....

Z0.indexOf(state) > -1 ? "Z0" : Z1.indexOf(state) > -1 ? "Z1" : Z2.indexOf(state) > -1 ? "Z2"  : .... : ""

Rainer
rg@delos-consulting.com

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Maybe you get an error notification in the preview like : Invalid value ... (string); must be a number" - don't consider it, just click done and save.

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hey @Rainer,

I tried this based off your example and no dice.

var state = @Pick City, State + "";
var Z0 = "CT, ME, MA, NH, NJ, RI, VT";
var Z1 = "DE, NY, PA";
var Z2 = "MD, NC, SC, VA, DC, WV";
var Z3 = "AL, FL, GA, MS, TN";
var Z4 = "IN, KY, MI, OH";
var Z5 = "IA, MN, MT, ND, SD, WI";
var Z6 = "IL, KS, MO, NE";
var Z7 = "AR, LA, OK, TX";
var Z8 = "AZ, CO, ID, NV, NM, UT, WY";
var Z9 = "CA, OR, WA, AK";

Z0.indexOf(state) > -1 ? "Z0" :
Z1.indexOf(state) > -1 ? "Z1" :
Z2.indexOf(state) > -1 ? "Z2" :
Z3.indexOf(state) > -1 ? "Z3" :
Z4.indexOf(state) > -1 ? "Z4" :
Z5.indexOf(state) > -1 ? "Z5" :
Z6.indexOf(state) > -1 ? "Z6" :
Z7.indexOf(state) > -1 ? "Z7" :
Z8.indexOf(state) > -1 ? "Z8" :
Z9.indexOf(state) > -1 ? "Z9" : "";

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

Hi Mitchell,

did you test what's the value of var state? Does var state show something like NY, FL, OH etc.?
What is the content of @Pick City, State + ""? What kind of field is it? Is it also a calculation field where you get the state from the location field?

I've tested my code and it works. I've grabbed the state from a location field like; the result I've put into the var state. Have no problems.

Rainer

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

The solution, I think:

Calculation to pull state from the google location field

var location = @Pick City, State + "";
var length = location.length;
var start = length - 7;
var end = length - 5;
var state = location.substring(start, end);
state

Calculation to decide the zone based off the state

var state = @Pick State;
var Z0 = "CT, ME, MA, NH, NJ, RI, VT";
var Z1 = "DE, NY, PA";
var Z2 = "MD, NC, SC, VA, DC, WV";
var Z3 = "AL, FL, GA, MS, TN";
var Z4 = "IN, KY, MI, OH";
var Z5 = "IA, MN, MT, ND, SD, WI";
var Z6 = "IL, KS, MO, NE";
var Z7 = "AR, LA, OK, TX";
var Z8 = "AZ, CO, ID, NV, NM, UT, WY";
var Z9 = "CA, OR, WA, AK";

Z0.indexOf(state) > -1 ? "Z0" :
Z1.indexOf(state) > -1 ? "Z1" :
Z2.indexOf(state) > -1 ? "Z2" :
Z3.indexOf(state) > -1 ? "Z3" :
Z4.indexOf(state) > -1 ? "Z4" :
Z5.indexOf(state) > -1 ? "Z5" :
Z6.indexOf(state) > -1 ? "Z6" :
Z7.indexOf(state) > -1 ? "Z7" :
Z8.indexOf(state) > -1 ? "Z8" :
Z9.indexOf(state) > -1 ? "Z9" : "";

Still waiting for the result to populate in all my records, but the feedback states the correct zone:

http://screencast.com/t/VhNJbJUvdp

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

@Rainer

I think we're very close to the solution above, but for the life of me I can't get the value to display... What do you think the hold up is?

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

Hi Mitchell,

as I've posted some days ago: Maybe you get an error notification in the preview like : Invalid value ... (string); must be a number" - don't consider it, just click done and save.

Ignore this notification, it works

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

@Rainer

What I'm saying is that it never populates the value in any of the records. Just stays blank.

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

Hi Mitchell,

in your last code you've posted here the var state = @pick state. So Z0.indexOf(state) looks if the value @pick state is in Z0.
But what's the value of @pick state? Is it the abbreviation of a state?

Everything you've written in your code before the line var state = @Pick State is useless, cause you define the var state new with as var state = @Pick State - and this value is taken in the if-conditions. So if the value isn't in the vars Z0-Z9 than you get "" and the field stays blank.

For an address in this format:

1600 Pennsylvania Avenue Northwest, Washington, DC 20500, USA

you can pick the state with one of the 4 example codes you'll find on the screenshot. They work for the format:

street, City, State followed by zip code

It doesn't depend if "USA" is in the address or not.

Rainer
rg@delos-consulting.com

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

@Rainer

@Pick State strips away the state from the google location field:

var location = @Pick City, State + "";
var length = location.length;
var start = length - 7;
var end = length - 5;
var startZip = length - 13;
var endZip = length - 11;
var state = location.substring(start, end);
if (isNaN(state)) {state} else {location.substring(startZip, endZip)}

What I don't understand is that the preview shows the correct zone value for the pick state... Why doesn't it show up when not in modify template mode?

Mitchell Ogden 0 votes
Comment actions Permalink
0
Avatar

That's strange.
Did you check the type of the calc field? Must be text type .
Some things which help sometimes to refresh the calc fields:
Enter some blanks in the calc field (in modify mode), look if preview shows correct result and save it. Maybe refrsh the item view
Refresh app view with all items.
Instead of "" at the end of teh code, try "ABC" and look what will be shown in item view.

Rainer

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

@Rainer .... You are the Man!!! I have learned a lot from you. I appreciate everything you do here. Seems the data type of a calculation field is decided the first time you save a calculation field. Knowing that has solved a lot of my issues with getting calculations to return the correct data type.

Cheers!

Mitch

Mitchell Ogden 0 votes
Comment actions Permalink