Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Last active March 2, 2018 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccnokes/cfd98570364965e1ef0009829126bf65 to your computer and use it in GitHub Desktop.
Save ccnokes/cfd98570364965e1ef0009829126bf65 to your computer and use it in GitHub Desktop.
Modulo use cases because math is hard
// 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
@ccnokes
Copy link
Author

ccnokes commented Mar 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment