Files
30-seconds-of-code/snippets/maintain-digits.md
Michael Goldspinner 599c0bea93 Maintain Digits
2018-01-11 20:45:42 -05:00

429 B

Maintain Digits

Use the modulo operator (%) to find values of single and tens digits. Find which ordinal pattern digits match. If digit is found in teens pattern, use teens ordinal.

const setDigits = ( int=0, len=1, dir=0 ) => {
    return dir > 0 ? int.toString().padStart(len, "0") : int.toString().padEnd(len, "0");
}
// setDigits(1, 2, 0) -> "10"
// setDigits(1, 2, 1) -> "01"
// setDigits(11, 2, 1) -> "11"