3vo

Problem 10. Number as Words / if else

3vo
Oct 25th, 2022
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = ['253'];
  2.  
  3. let print = this.print || console.log;
  4. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  5. //get the input
  6. let stringInput = gets();
  7. //reserve empty string to fill for result
  8. let resultString = '';
  9.  
  10. //in case in 3 digit string
  11. if (stringInput.length === 3) {
  12.     //get the input by index and convert them to numbers in 3 digit string
  13.     let firstDigitUnique = +stringInput[0];
  14.     let secondDigit = +stringInput[1];
  15.     let thirdDigit = +stringInput[2];
  16.     //write first digit in words
  17.     if (firstDigitUnique === 1) {
  18.         resultString += 'one hundred '
  19.     } else if (firstDigitUnique === 2) {
  20.         resultString += 'two hundred ';
  21.     } else if (firstDigitUnique === 3) {
  22.         resultString += 'three hundred ';
  23.     } else if (firstDigitUnique === 4) {
  24.         resultString += 'four hundred ';
  25.     } else if (firstDigitUnique === 5) {
  26.         resultString += 'five hundred ';
  27.     } else if (firstDigitUnique === 6) {
  28.         resultString += 'six hundred ';
  29.     } else if (firstDigitUnique === 7) {
  30.         resultString += 'seven hundred ';
  31.     } else if (firstDigitUnique === 8) {
  32.         resultString += 'eight hundred ';
  33.     } else if (firstDigitUnique === 9) {
  34.         resultString += 'nine hundred ';
  35.     }
  36.     // in case if second and third index are 0
  37.     if (secondDigit === 0 && thirdDigit === 0) {
  38.         stringInput = '';
  39.     } else {
  40.         resultString += 'and';
  41.         stringInput = secondDigit + '' + thirdDigit;
  42.     }
  43. }
  44. //in case 2 digit string
  45. if (stringInput.length === 2) {
  46.     let num = +stringInput;
  47.     //convert input to number in unique word cases from 1 to 19
  48.     if (num < 20) {
  49.         if (num === 1) {
  50.             resultString += ' one';
  51.         } else if (num === 2) {
  52.             resultString += ' two';
  53.         } else if (num === 3) {
  54.             resultString += ' three';
  55.         } else if (num === 4) {
  56.             resultString += ' four';
  57.         } else if (num === 5) {
  58.             resultString += ' five';
  59.         } else if (num === 6) {
  60.             resultString += ' six';
  61.         } else if (num === 7) {
  62.             resultString += ' seven';
  63.         } else if (num === 8) {
  64.             resultString += ' eight';
  65.         } else if (num === 9) {
  66.             resultString += ' nine';
  67.         } else if (num === 10) {
  68.             resultString += ' ten';
  69.         } else if (num === 11) {
  70.             resultString += ' eleven';
  71.         } else if (num === 12) {
  72.             resultString += ' twelve';
  73.         } else if (num === 13) {
  74.             resultString += ' thirteen';
  75.         } else if (num === 14) {
  76.             resultString += ' fourteen';
  77.         } else if (num === 15) {
  78.             resultString += ' fifteen';
  79.         } else if (num === 16) {
  80.             resultString += ' sixteen';
  81.         } else if (num === 17) {
  82.             resultString = ' seventeen';
  83.         } else if (num === 18) {
  84.             resultString += ' eighteen';
  85.         } else if (num === 19) {
  86.             resultString += ' nineteen';
  87.         }
  88.     } else {
  89.         let firstDigit = +stringInput[0];
  90.         let secondDigit = +stringInput[1];
  91.  
  92.         if (firstDigit === 2) {
  93.             resultString += ' twenty';
  94.         } else if (firstDigit === 3) {
  95.             resultString += ' thirty';
  96.         } else if (firstDigit === 4) {
  97.             resultString += ' forty';
  98.         } else if (firstDigit === 5) {
  99.             resultString += ' fifty';
  100.         } else if (firstDigit === 6) {
  101.             resultString += ' sixty';
  102.         } else if (firstDigit === 7) {
  103.             resultString += ' seventy';
  104.         } else if (firstDigit === 8) {
  105.             resultString += ' eighty';
  106.         } else if (firstDigit === 9) {
  107.             resultString += ' ninety';
  108.         }
  109.             if (secondDigit === 0) {
  110.                 stringInput = '';
  111.             } else {
  112.                 stringInput = '' + secondDigit;
  113.             }
  114.     }
  115.  
  116. }
  117. //in case 1 number string
  118. if (stringInput.length === 1 ) {
  119.     let num = +stringInput;
  120.  
  121.     if (num === 0) {
  122.         resultString += 'Zero';
  123.     } else if (num === 1) {
  124.         resultString += ' one';
  125.     } else if (num === 2) {
  126.         resultString += ' two';
  127.     } else if (num === 3) {
  128.         resultString += ' three';
  129.     } else if (num === 4) {
  130.         resultString += ' four';
  131.     } else if (num === 5) {
  132.         resultString += ' five';
  133.     } else if (num === 6) {
  134.         resultString += ' six';
  135.     } else if (num === 7) {
  136.         resultString += ' seven';
  137.     } else if (num === 8) {
  138.         resultString += ' eight';
  139.     } else if (num === 9) {
  140.         resultString += ' nine';
  141.     }
  142. }
  143.  
  144. resultString = resultString.trim();
  145. let firstLetter =  resultString.charAt(0).toUpperCase();
  146. resultString = firstLetter + resultString.substring(1);
  147.  
  148. console.log(resultString);
  149.  
Advertisement
Add Comment
Please, Sign In to add comment