Select to view content in your preferred language

Use arcade to check if a number is even or odd

300
3
Jump to solution
08-14-2024 06:20 AM
CameronLacelle
Frequent Contributor

Is there a way in arcade to take a value and determine if it is an odd or even number? I have an if statement within a for loop and want the function to perform differently when my var i is even versus when it is odd. This is for the purpose of colouring table rows differently as I am building an HTML string from my arcade script.

0 Kudos
2 Solutions

Accepted Solutions
CodyPatterson
Frequent Contributor

Hey @CameronLacelle 

This script here should help, this uses the % operator to check the remainder of the number divided by 2, if zero, then it is even, else it is odd.

var number = 7
var isEven = (number % 2 == 0)
if (isEven) {
    return "Even"
} else {
    return "Odd"
}

You'd replace the return with your function and you'll be set!

https://developers.arcgis.com/arcade/guide/operators/

Cody

View solution in original post

berniejconnors
Frequent Contributor

Divide by 2 and check for a remainder.  If the remainder is 0 its and even number.  Use the Modulus (%) operator in Arcade to get the remainder.

Bernie.

View solution in original post

3 Replies
CodyPatterson
Frequent Contributor

Hey @CameronLacelle 

This script here should help, this uses the % operator to check the remainder of the number divided by 2, if zero, then it is even, else it is odd.

var number = 7
var isEven = (number % 2 == 0)
if (isEven) {
    return "Even"
} else {
    return "Odd"
}

You'd replace the return with your function and you'll be set!

https://developers.arcgis.com/arcade/guide/operators/

Cody

CameronLacelle
Frequent Contributor

Thank you

berniejconnors
Frequent Contributor

Divide by 2 and check for a remainder.  If the remainder is 0 its and even number.  Use the Modulus (%) operator in Arcade to get the remainder.

Bernie.