Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let words = {
- 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"
- }
- for (let i = 1; i < 10; i++) {
- words[i * 100] = words[i] + " hundred";
- }
- // 1 -> "one" -> 3
- // 10 -> "ten" -> 3
- // 19 -> "nineteen" -> 8
- // 367 -> "three hundred and sixty seven" -> 5 + 7 + 3 + 5 + 5
- // For the numbers 1 - 1000
- // sum the total number of characters, excluding spaces
- // For numbers > 99 not divisible by 100, an "and" should separate the hundreds from the tens/ones.
- function getHundreds(num) {
- if(num < 100) return '';
- return words[Math.floor(num / 100)] + 'hundred';
- };
- function getTens(num) {
- let restTens = num % 100;
- // number is one of 100, 200, 300..
- if (restTens === 0) return '';
- if(restTens > 20) {
- let firstPart = words[Math.floor(restTens / 10) * 10];
- let secondPart = words[restTens % 10];
- return firstPart + secondPart;
- }
- // under 20 does not require assembly
- return words[restTens];
- }
- function getStringLength(num) {
- if(num === 1000) return 'onethousand'.length
- let hundreds = getHundreds(num);
- let tens = getTens(num);
- if(hundreds && tens) {
- console.log(hundreds + "and" + tens);
- return hundreds.length + "and".length + tens.length;
- };
- if(hundreds) {
- console.log(hundreds + tens)
- return tens.length + hundreds.length
- };
- console.log(tens);
- return tens.length;
- }
- let sum = 0;
- for (let n = 1; n < 1000; n++) {
- sum += getStringLength(n);
- }
- console.log('succes:',sum + 11 === 21124);
Add Comment
Please, Sign In to add comment