Last active
March 2, 2018 17:55
-
-
Save ccnokes/cfd98570364965e1ef0009829126bf65 to your computer and use it in GitHub Desktop.
Modulo use cases because math is hard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find if number is even or odd | |
const isEven = val => val % 2 === 0; | |
isEven(11); //false | |
isEven(22); //true | |
// do time related things... | |
const formatMovieTime = val => { | |
const hours = Math.floor(val / 60); //get the hours, discard any remainder via `floor` | |
const mins = val % 60; //get the remainder of minutes left over as an integer | |
return `${hours}:${mins < 10 ? '0' + mins : mins}`; | |
}; | |
//let's say these are movie run times, which are always in just minutes for some reason | |
formatMovieTime(121); // 2:01 | |
formatMovieTime(165); // 2:45 | |
formatMovieTime(180); // 3:00 | |
// convert military time to normal, human readable time | |
const militaryTimeToNormalTime = str => { | |
const split = str.split(':'); | |
return `${split.shift() % 12}:${split.pop()}` | |
}; | |
militaryTimeToNormalTime('13:59') // 1:59 | |
militaryTimeToNormalTime('2:01') // 2:01 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more ideas: https://betterexplained.com/articles/fun-with-modular-arithmetic/