Arcade When function is not working in a popup

357
2
Jump to solution
10-04-2022 05:57 AM
Labels (1)
KenBuja
MVP Esteemed Contributor

I was helping someone with a script to split dates into quarters using a When function. This code works correctly when testing it, but the popup does not return the same results. The When function is not returning the correct value in the popup.

var theDate = Date();

var theMonth = Month(theDate); //note Month is zero-based (Jan = 0)
var quarter;

//This always returns 4 in the popup, regardless of the date
//when (theMonth < 3, quarter = 1,
//      theMonth < 6, quarter = 2,
//      theMonth < 9, quarter = 3,
//      quarter = 4}

when (theMonth < 3, quarter = 1,
      theMonth < 6, quarter = 2,
      theMonth < 9, quarter = 3,
      theMonth < 12, quarter = 4,
      quarter = "Fail");

return `Today's date: ${Text(theDate,"MMMM D, YYYY")}
        Today's month: ${theMonth}
        Today's quarter: ${quarter}`;

When I run the Test in the code editor, it gives me the correct result.

2022-10-04_8-56-28.png

However, it does not in the popup.

2022-10-04_8-54-00.png

Is this a new bug?

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You have to declare the variable outside of When(). When() doesn't change any variables, it only returns a value.

var quarter = when (theMonth < 3, 1,
      theMonth < 6, 2,
      theMonth < 9, 3,
      theMonth < 12, 4,
      "Fail");

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

You have to declare the variable outside of When(). When() doesn't change any variables, it only returns a value.

var quarter = when (theMonth < 3, 1,
      theMonth < 6, 2,
      theMonth < 9, 3,
      theMonth < 12, 4,
      "Fail");

Have a great day!
Johannes
KenBuja
MVP Esteemed Contributor

Thanks! It's odd that it returned the correct value in the Test, though

0 Kudos