I am trying to practice objects and functions (specifically closures & higher order functions) in javascript. There is no specific reason why I chose this program but it struck my mind to work out this one.
The below code is available at my Github and it converts the number to words.
Kindly provide your feedback and suggest which parts of the code can be improved or modified.
// Author : Harish Narayanan
// Date : 15 May 2015
// Description : Code to convert number to words
var ones_teens = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
];
var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
var scales = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion',
'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion'
];
function toWords(n) {
var line;
if (isNaN(n) || n > 999) {
return null;
}
if (n == 0) {
line = "";
} else if (n < 20) {
line = ones_teens[n];
} else if (n < 100) {
line = tens[(n / 10) | 0];
if (n % 10) {
line += "-" + ones_teens[n % 10];
}
} else {
line = ones_teens[(n / 100) | 0] + " hundred " + toWords(n % 100);
}
return line.trim();
}
function chunkify(n) {
var numbers = [];
while (n > 0) {
numbers[numbers.length] = n % 1000;
n = Math.floor(n / 1000);
}
return numbers;
}
function scalify(item, index) {
if (item) {
return item + " " + scales[index];
}
return "";
}
function notEmpty(item) {
return !!item; // using double ! returns boolean value. !!item is not same as item.
}
console.log(chunkify(999999999999999).map(toWords).map(scalify).filter(notEmpty).reverse().join(", "));