Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

24 Remainder (Modulus)

MDN: Expressions and Operators: Remainder

The remainder (a.k.a. modulus) operator will return the remainder when dividing two numbers which do not divide evenly and return 0 when they do divide evenly. In Scripter, this is critical to doing math with MIDI pitches and knowing which division the playhead is in with TimingInfo.

const CHROMATIC_SCALE = 12;
var tonic = 61;
var aFloat = tonic / CHROMATIC_SCALE; // 5.083333333333333
var remainder = tonic % CHROMATIC_SCALE; // 1
const MIDDLE_C = 60; // change back to an evenly divisible number
remainder = MIDDLE_C % CHROMATIC_SCALE; // 0