We're going to write a function numberToEnglish
that takes a non-negative integer as input and returns a string containing the English equivalent.
For example:
numberToEnglish(100); // => 'one hundred'
numberToEnglish(5491); // => 'five thousand four hundred ninety one'
numberToEnglish(12345); // => 'twelve thousand three hundred forty five'
numberToEnglish(9456012); // => 'nine million four hundred fifty six thousand twelve'
Remember, if you're getting lost in the code:
- Slow down
- Work out simple examples you know you can do without a computer
- Pay close attention to what you're doing to solve the problem and why
You might find these built-in functions helpful:
This exercise is complex enough that it's worth tackling special cases first before we try to tackle the general case.
This iteration is complete. See numberToEnglish.js.
We wrote a helper function called smallNumberToEnglish
that takes in a number below 20 and returns the appropriate string. The function throws an error if you try to give it a number larger than 19.
Rather than using 20 lines of if/else
statements, it uses an array of English names as a lookup table. That is:
- The string at index
0
of the lookup array is'zero'
- The string at index
1
of the lookup array'one
- The string at index
14
of the lookup array is'fourteen'
- etc.
When dealing with many consecutive numbers, an array is often clearer and more concise than a long list of if/else
conditionals.
Add support for any one-or-two-digit number, not just those smaller than 20. You are free to achieve this however you want:
- Modify
smallNumberToEnglish
to support it directly - Create another helper function to handle numbers 20 and up that calls
smallNumberToEnglish
to do its work - Something else of your design
If you find yourself writing logic to handle the case where num
is 0
in many locations, consider handling it right at the top of numberToEnglish
. If you do that then you can guarantee that num > 0
when you call smallNumberToEnglish(num)
.
Add support for numbers smaller than 1,000. What would it take to to modify smallNumberToEnglish
to handle 3-digit numbers? What about handling
Add support for numbers smaller than 1,000,000.
By this point, you should be within striking distance of the the general pattern. Add support for more powers of 1000 (million, billion, trillion, etc.) You choose how large of a number to support.