Created
February 11, 2020 17:01
-
-
Save danielyaa5/b76f86497facf49a4a70ad5197ab4c23 to your computer and use it in GitHub Desktop.
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
// (224) 577 5898 | |
const getTens = tens => ['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninetey'][tens - 1] | |
const below20 = integer => ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eightteen', 'Nineteen'][integer - 1] | |
const addWords = (result, words) => result.length === 0 ? words : `${result} ${words}`; | |
const solution = (integer) => { | |
if (integer === 0) return 'Zero'; | |
if (integer < 20) return below20(integer); | |
const thousands = Math.floor(integer / 1000); | |
const hundreds = Math.floor((integer - (thousands * 1000)) / 100); | |
const tens = Math.floor((integer - (thousands * 1000) - (hundreds * 100)) / 10); | |
const tens2 = integer - (thousands * 1000) - (hundreds * 100); | |
const ones = integer - (thousands * 1000) - (hundreds * 100) - (tens * 10); | |
let result = ''; | |
if (thousands) { | |
result = addWords(result, `${solution(thousands)} Thousand`); | |
} | |
if (hundreds) { | |
result = addWords(result, `${below20(hundreds)} Hundred`); | |
} | |
if (tens2 < 20) { | |
result = addWords(result, below20(tens2)); | |
return result; | |
} | |
if (tens) { | |
result = addWords(result, getTens(tens)); | |
} | |
if (ones) { | |
result = addWords(result, below20(ones)); | |
} | |
return result; | |
} | |
console.log(solution(3542), '3542') | |
console.log(solution(11), '11') | |
console.log(solution(123915), '123915') | |
console.log(solution(99999), '99999') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment