let nowMonth = 2; // March
let futureMonth = nowMonth + 18;
console.log( months[ futureMonth % months.length ] ); // still outputs September, even though there isn't a 20th month
Worth noting that lots of languages refer to their months from 0-11, presumably for cool tricks like this.
I'm doing this right now in an app where each created item is assigned a color with ~15 predefined colors after which it will loop back around. Previously I was randomly generating a color and often getting similar shades back to back.
101
u/nqvst Mar 18 '19
items[n % items.length]gets an item from the list and wraps around to the start if n is larger than the list