Created
April 24, 2018 00:35
-
-
Save tspoff/30ac2642155dfd7447fdad3ccc4635cf to your computer and use it in GitHub Desktop.
Converts integer input to a numerical string (i.e. how you would write numbers on a check - e.g. "One hundred seventy-four");
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 ZERO = "0"; | |
const ONE = "1"; | |
const HUNDRED_AS_WORD = "hundred"; | |
const ZERO_AS_WORD = "zero"; | |
const digitMap = { | |
"0": "", | |
"1": "one", | |
"2": "two", | |
"3": "three", | |
"4": "four", | |
"5": "five", | |
"6": "six", | |
"7": "seven", | |
"8": "eight", | |
"9": "nine", | |
"10": "ten", | |
"11": "eleven", | |
"12": "twelve", | |
"13": "thirteen", | |
"14": "fourteen", | |
"15": "fifteen", | |
"16": "sixteen", | |
"17": "seventeen", | |
"18": "eightteen", | |
"19": "nineteen" | |
} | |
const tensPlaceMap = { | |
"2": "twenty", | |
"3": "thirty", | |
"4": "fourty", | |
"5": "fifty", | |
"6": "sixty", | |
"7": "seventy", | |
"8": "eighty", | |
"9": "ninety" | |
} | |
const placeValueMap = { | |
"4": "thousand", | |
"5": "thousand", | |
"6": "thousand", | |
"7": "million", | |
"8": "million", | |
"9": "million", | |
"10": "billion", | |
"11": "billion", | |
"12": "billion", | |
"13": "trillion", | |
"14": "trillion", | |
"15": "trillion", | |
"16": "quadrillion", | |
"17": "quadrillion", | |
"18": "quadrillion" | |
} | |
digitNameLookup = (input) => { | |
return digitMap[input]; | |
} | |
tensPlaceLookup = (input) => { | |
return tensPlaceMap[input]; | |
} | |
getPlaceValue = (digits, index) => { | |
if (index <= 3) { | |
return ""; | |
} else { | |
if (digitsToInt(digits) === 0) { | |
return ""; | |
} else { | |
const placeMapIndex = index; | |
return placeValueMap[placeMapIndex]; | |
} | |
} | |
} | |
integerToNumericalString = (input) => { | |
let digits = input.toString().split(""); | |
console.log(input + ": " + digitsToString(digits)); | |
}; | |
digitsToInt = (digits) => { | |
return Number(digits.slice(0).join("")); | |
} | |
digitsToString = (digits) => { | |
let digitStrings = []; | |
if (digits[0] === ZERO) { | |
digitStrings[0] = ZERO_AS_WORD; | |
} else { | |
for (let i = digits.length; i > 0; i -= 3) { | |
const startIndex = Math.max(i - 3, 0); | |
const currentDigits = digits.slice(startIndex, i); | |
let digitString = threeDigits(currentDigits); | |
const placeValueString = getPlaceValue(currentDigits, digits.length - i + 1); | |
if (placeValueString != "") { | |
digitString = digitString + " " + placeValueString; | |
} | |
if (digitsToInt(currentDigits) !== 0) { | |
digitStrings.push(digitString); | |
} | |
} | |
} | |
let result = ""; | |
digitStrings = digitStrings.reverse(); | |
for (let i = 0; i < digitStrings.length; i++) { | |
result += digitStrings[i] + " "; | |
} | |
//Capitalize the first letter | |
result = result[0].toUpperCase() + result.substring(1); | |
//Remove trailing spaces | |
result.trim(); | |
return result; | |
} | |
threeDigits = (input) => { | |
if (input.length < 3) { | |
return twoDigits(input); | |
} else if (input[0] === ZERO) { | |
return twoDigits(input.slice(1, 3)); | |
} else { | |
return digitNameLookup(input[0]) + " " + HUNDRED_AS_WORD + " " + twoDigits(input.slice(1, 3)); | |
} | |
} | |
twoDigits = (input) => { | |
const finalElement = input.length - 1; | |
//0 - 9 | |
if (input.length < 2 || input[0] === ZERO) { | |
return digitNameLookup(input[finalElement]); | |
} | |
//10 - 19 | |
else if (input[0] === ONE) { | |
return digitNameLookup(input[0] + input[1]); | |
} | |
//20 - 99 | |
else { | |
if (input[finalElement] === ZERO) { | |
return tensPlaceLookup(input[0]); | |
} else { | |
return tensPlaceLookup(input[0]) + "-" + digitNameLookup(input[1]); | |
} | |
} | |
} | |
const inputs = [ | |
0, | |
1, | |
9, | |
18, | |
23, | |
99, | |
877, | |
1000, | |
1001, | |
1010, | |
1101, | |
1110, | |
1111, | |
12345678, | |
10000000, | |
10000000001, | |
10000000101, | |
10001111, | |
98080800, | |
111111111111111 | |
]; | |
for (let i in inputs) { | |
integerToNumericalString(inputs[i]); | |
} | |
integerToNumericalString(Number.MAX_SAFE_INTEGER); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment