Select to view content in your preferred language

# Use arcade to check if a number is even or odd

300
3
08-14-2024 06:20 AM
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.

2 Solutions

Accepted Solutions
Frequent Contributor

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!

Cody

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.

3 Replies
Frequent Contributor

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!