Last active
September 5, 2016 01:59
-
-
Save jdfm/d77ea54c45087c70c057a0a71e167138 to your computer and use it in GitHub Desktop.
ES6 Exploration: leftPad for natural numbers
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
const leftPad = ( | |
( input=0, minOutputLength=0 ) => | |
String( input ).replace( /^(\d+)$/, ( match, $1='' ) => | |
'0'.repeat( Math.max( minOutputLength - $1.length, 0 ) ) + $1 | |
) | |
) | |
console.log(leftPad(10000, 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment