Created
March 3, 2020 13:51
-
-
Save code2be/1e8869813f67a8d90fe2e60ed75c19c5 to your computer and use it in GitHub Desktop.
integerToEnglishWords pretty code by psr@CodeSignal
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
// Simple JS code to convert a number into English Words. | |
// Copied from CodeSignal Solutions, Coded by psr < https://app.codesignal.com/profile/psr > | |
a = ` One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen`.split` ` | |
b = ` Twen Thir For Fif Six Seven Eigh Nine`.split` ` | |
w = ` Thousand Million Billion`.split` ` | |
f = integerToEnglishWords = ( | |
n, | |
k = 0, | |
o = n % 1000, | |
h = o / 100 | 0, | |
c = o % 100, | |
y = c % 10, | |
d = c / 10 | 0 | |
) => n | |
? `${ | |
f(n / 1000 | 0, k + 1) | |
} ${a[h] && a[h] + ` Hundred`} ${ | |
c < 15 | |
? a[c] | |
: c < 20 | |
? b[c % 10] + `teen` | |
: b[d] + `ty ` + a[y] | |
} ${o ? w[k] : ``} ` | |
.trim() | |
.replace(/ +/g, ` `) | |
: k ? `` : `Zero` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment