Last active
January 26, 2022 16:58
-
-
Save pseudosavant/ca1863df1e1fda4899c7ed453c247609 to your computer and use it in GitHub Desktop.
Divide any number/string every nth characters
This file contains hidden or 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 divide = (str, divider, n) => String(str) | |
.split('') | |
.reverse() | |
.reduce((acc, cv, i) => cv + (i % n === 0 ? divider : '') + acc); | |
let num = 4000000; | |
divide(num, '|', 2); // 4|00|00|00 | |
divide(num, ',', 3); // 4,000,000 | |
divide(num, '-', 1); // 4-0-0-0-0-0-0 | |
let cc = 1000200030004000; | |
divide(cc, '-', 4); // 1000-2000-3000-4000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment