Modulo operator

Category:Math

Returns the remainder of a division problem.

Examples

// MOD returns the remainder of a division problem
var x=10;
var y=3;
console.log(x%y);

Odd or Even?

One common use for modulo is to determine if a value is odd or even.

                            
textLabel("title", "Is your number odd or even?");
textInput("input", "");
setProperty("input", "placeholder", "Enter Your Number");
button("button", "Odd or Even?");
textLabel("display", "");
onEvent("button", "click", function( ) {
  var num = getNumber("input");
  if (num%2==0) {
    setText("display", "It's even!");
  } else {
    setText("display", "It's odd!");
  }
});
button("reset", "Reset");
onEvent("reset", "click", function( ) {
  setText("input", "");
  setProperty("input", "placeholder", "Enter Your Number");
  setText("display", "");
});
                        

Syntax

x % y

Parameters

NameTypeRequired?Description
x Number The dividend of the division, the number being divided.
y Number The divisor of the division, the number x is being divided by.

Returns

The remainder of the division x / y

Found a bug in the documentation? Let us know at documentation@code.org