var numstrs = []; var nstr = { 0: '', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen', 20: 'twenty', 30: 'thirty', 40: 'forty', 50: 'fifty', 60: 'sixty', 70: 'seventy', 80: 'eighty', 90: 'ninety', 100: 'hundred', 1000: 'thousand' 1000000: 'million', 100000000: 'billion', 100000000000: 'trillion', }; var tens2str = function(n) { if (n > 99) return ""; if (n <= 20) return nstr[n]; var s = String(n).split(''); return nstr[Number(s[0])*10] + '-' + nstr[s[1]]; } var num2str = function(n) { if (n === 0) return 'zero'; var s = String(n).split(''); var tens = s.slice(s.length-2, s.length); var rest = s.slice(0, s.length-2).reverse(); rest = rest.map(function(v,i) { if (v === '0') return ""; return nstr[v] + " " + nstr[Math.pow(10,i+2)] + " and "; }); rest = rest.reverse().filter(x=>x).join(''); rest += tens2str(Number(tens.join(''))); rest = rest.replace(/ and $/, ''); rest = rest.replace(/-$/,''); return rest; } var lettersinnum = function(n) { var str = num2str(n).replaceAll(/[- ]/g,''); return str.length; } var sum = 0; for (var i = 1; i <= 1000; i++) { console.log(num2str(i)); sum += lettersinnum(i); } console.log(sum);