Arcade / When statement

4473
4
02-06-2019 09:57 AM
RichardQuodomine1
New Contributor III

OK, so I have an Arcade script that is looking for a closed parenthesis, but for the life of me, I can't figure out where. I've added 2, just for illustration - but neither seems to work on line 3. Admittedly, I don't do a lot of Arcade, so any help appreciated. 

Tags (3)
0 Kudos
4 Replies
NickDierks1
Occasional Contributor II

I'm still learning Arcade myself, but it looks to me like you're definitely missing an open parenthesis on line 3. Try when((day_ = 1), "Sunday", "n/a")

XanderBakker
Esri Esteemed Contributor

Hi Richard Quodomine ,

I see a couple of errors in the expression and because of that I'm not entirely clear what you are trying to do. Let's include the code after the correction made by Nick Dierks to sove the missing parenthesis:

function RetWeekDay(text){
    var day_ = $feature.DAY_OF_WEE;
    var DOW = When((day_ = 1), "Sunday", "n/a");
    DOW = RetWeewkDay;
    return RetWeekDay;
}

Let me list some of the errors:

  • on line 1 you define a parameter that is actually an existing function itself. You should avoid that. A complete list of functions can be found here: Arcade Function Reference | ArcGIS for Developers 
  • This parameter is never used, so why define it?
  • On line 3 the function When requires a condition. If you compare day_ with 1, you should use double equal signs, so: day_ == 1
  • On line 4 you replace the value just assigned to DOW with a call to the same function not specifying the parameter (this could potentially result in an infinite loop)
  • On line 5 you return the same function not specifying the parameter which also could result in an infinite loop.
  • When using a function you should call it in order for it to be executed. 

If you want to return the text "Sunday" when your attribute DAY_OF_WEE is 1, and  "n/a" in all other cases, you can do something like:

if ($feature.DAY_OF_WEE == 1) {
    return "Sunday";
} else {
    return "n/a";
}
RichardQuodomine1
New Contributor III

In order:

First, thank you both. 

Second, I did have the double parens up in my first rendition. It kept looking for a close, but I had two. 

Third, I should have been clearer, the When statement will go through all 7 days, with a 1 = Sunday, 2 = Monday, etc. I think I whiffed on the ==. I'll get back to all of you with the more complete code, after testing. Thank you!

0 Kudos
RichardQuodomine1
New Contributor III

So here it is:
var day_ = $feature.DAY_OF_WEE;
var DOW = when((day_ == 1), "Sunday", day_ == 2, "Monday", day_ == 3, "Tuesday", day_ == 4, "Wednesday", day_ == 5, "Thursday", day_ == 6, "Friday", day_ == 7, "Saturday", "n/a");
DOW

Thanks to both of you for the help. I was just doing a calculated field to get get a good symbology for some data I had. I really appreciate the guidance.