Created
October 27, 2015 23:19
-
-
Save trys/5d87ed612d3bd362e14d to your computer and use it in GitHub Desktop.
Problem Seventeen
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
function nameNumber( count ) { | |
var ones = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ], | |
tens = [ 'zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; | |
// Thousands | |
if ( count === 1000 ) { | |
return 'one thousand'; | |
} | |
// Under 20 | |
if ( count < 20 ) { | |
return ones[ count ]; | |
} else { | |
var countString = count + '', | |
countSplit = countString.split( '' ); | |
// Under 100 | |
if ( count < 100 ) { | |
return count % 10 === 0 ? tens[ countSplit[ 0 ] ] : tens[ countSplit[ 0 ] ] + ' ' + ones[ countSplit[ 1 ] ]; | |
} else { | |
var countTens = parseInt( countSplit[ 1 ] + '' + countSplit[ 2 ] ), | |
countPre = ones[ countSplit[ 0 ] ] + ' hundred'; | |
// Hundreds | |
if ( count % 100 === 0 ) { | |
return countPre; | |
} else { | |
// Under 'twenties' per hundred | |
if ( parseInt( countTens ) < 20 ) { | |
return countPre + ( count === 100 ? '' : ' and ' + ones[ countTens ] ); | |
} else { | |
// 'Tens' per hundred | |
if ( count % 10 === 0 ) { | |
return countPre + ' and ' + tens[ countSplit[ 1 ] ]; | |
} else { | |
return countPre + ' and ' + ( countSplit[ 1 ] !== '0' ? tens[ countSplit[ 1 ] ] : '' ) + ' ' + ones[ countSplit[ 2 ] ]; | |
} | |
} | |
} | |
} | |
} | |
return ''; | |
} | |
var counter = 0; | |
for (var i = 1; i <= 1000; i++) { | |
counter += nameNumber( i ).replace(/ /g,'').length; | |
} | |
document.write( counter ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment