Created
October 20, 2017 20:30
-
-
Save joshleichtung/1e9724a6ce06258c33ed0a3e5ea9dea8 to your computer and use it in GitHub Desktop.
Number to word Conversion
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
| // This is the text editor interface. | |
| // Anything you type or change here will be seen by the other person in real time. | |
| // Given a number 3141592654, write a code to translate this to Three Billion One Hundred Forty One Million Five Hundred Ninety Two Thousand Six Hundred Fifty Four. Your code should be able to translate any given number where 0 < number <= 9,999,999,999. | |
| const numberConversion = { | |
| 1000000000: "Billion", | |
| 1000000: "Million", | |
| 1000: "Thousand", | |
| 100: "Hundred", | |
| 90: "Ninety", | |
| 80: "Eighty", | |
| 70: "Seventy", | |
| 60: "Sixty", | |
| 50: "Fifty", | |
| 40: "Forty", | |
| 30: "Thirty", | |
| 20: "Twenty", | |
| 19: "Nineteen", | |
| 18: "Eighteen", | |
| 17: "Seventeen", | |
| 16: "Sixteen", | |
| 15: "Fifteen", | |
| 14: "Fourteen", | |
| 13: "Thirteen", | |
| 12: "Twelve", | |
| 11: "Eleven", | |
| 10: "Ten", | |
| 1: "One", | |
| 2: "Two", | |
| 3: "Three", | |
| 4: "Four", | |
| 5: "Five", | |
| 6: "Six", | |
| 7: "Seven", | |
| 8: "Eight", | |
| 9: "Nine", | |
| 0: "" | |
| } | |
| function numberToWords(num) { | |
| if(num < 1) return "Zero" | |
| let result = '' | |
| for(let i = 1; Math.floor(num / i) > 0; i *= 1000) { | |
| result = threeDigitToWords(Math.floor(num / i) % 1000) | |
| + (i > 1 ? ' ' + numberConversion[i] + ' ' : '') | |
| + result | |
| } | |
| return result | |
| } | |
| function threeDigitToWords(num) { | |
| result = '' | |
| if(Math.floor(num / 100)){ | |
| result += ' ' + numberConversion[Math.floor(num / 100)] + ' Hundred' | |
| } | |
| num = Math.floor(num % 100) | |
| if(num > 20) { | |
| result += ' ' + numberConversion[Math.floor(num - num % 10)] | |
| num = num % 10 | |
| } | |
| return (result + ' ' + numberConversion[num]).trim() | |
| } | |
| console.log(numberToWords(3141592654)) | |
| console.log(numberToWords(3141592654) === 'Three Billion One Hundred Forty One Million Five Hundred Ninety Two Thousand Six Hundred Fifty Four') | |
| for(let i = 1; i < 9999999999; i *= 10) { | |
| let num = Math.floor(Math.random() * i) | |
| console.log("Num: ", num, "Words: ", numberToWords(num)) | |
| } | |
| // console.log(threeDigitToWords(400)) | |
| //************* | |
| // Maha - I really enjoyed meeting with you yesterday. I had time to go ahead and finish the problem today. While this works for numbers inside the given domain, | |
| // it can easily be expanded to work for an arbitrarily large number (as long as it can be represented by JS as a Number). The numberConversion object just needs to be expanded | |
| // to account for trillion, quadrillion, etc... {1000000000000: "Trillion", 1000000000000000: "Quadrillion"}. | |
| // | |
| // Thanks again. Looking forward to hearing back from you. | |
| //************* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment