Maintain Digits
This commit is contained in:
14
snippets/maintain-digits.md
Normal file
14
snippets/maintain-digits.md
Normal file
@ -0,0 +1,14 @@
|
||||
### 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.
|
||||
|
||||
```js
|
||||
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"
|
||||
```
|
||||
Reference in New Issue
Block a user