Skip to content

Instantly share code, notes, and snippets.

@huttj
Last active August 29, 2015 14:12
Show Gist options
  • Save huttj/28db5a0af463f046381a to your computer and use it in GitHub Desktop.
Save huttj/28db5a0af463f046381a to your computer and use it in GitHub Desktop.
Turn a sequence of numbers into a sequence of words
var lookSaySequence = (function() {
var words = {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine'
};
var re = new RegExp('0123456789'.split('').join('+|') + '+', 'g');
return function(num) {
if (typeof num === 'undefined') return;
num = ''+num;
var arry = num.match(re);
var result = arry.map(function(n) {
var end = n.length === 1 ? '' : 's';
return words[n.length] + ' ' + words[n.charAt(0)] + end;
});
if (result.length === 1) return result[0];
if (result.length === 2) return result[0] + ' and ' + result[1];
return result.slice(0, result.length - 1).join(', ') + ', and ' + result.slice(-1);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment