Advertisement
dabidabidesh

9.Numbers as Words

Feb 17th, 2023
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = [
  2.   '290',
  3. ];
  4.  
  5. let p = console.log;
  6. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  7.  
  8. let numberStr = gets();
  9. let len = numberStr.length;
  10.  
  11. let unitsObj = {
  12.   '0': 'zero',
  13.   '1': 'one',
  14.   '2': 'two',
  15.   '3': 'three',
  16.   '4': 'four',
  17.   '5': 'five',
  18.   '6': 'six',
  19.   '7': 'seven',
  20.   '8': 'eight',
  21.   '9': 'nine',
  22. };
  23.  
  24. let dozensObj = {
  25.   '10': 'ten',
  26.   '20': 'twenty',
  27.   '30': 'thirty',
  28.   '40': 'forty',
  29.   '50': 'fifty',
  30.   '60': 'sixty',
  31.   '70': 'seventy',
  32.   '80': 'eighty',
  33.   '90': 'ninety',
  34. };
  35.  
  36. let numbers10to19 = {
  37.   '10': 'ten',
  38.   '11': 'eleven',
  39.   '12': 'twelve',
  40.   '13': 'thirteen',
  41.   '14': 'fourteen',
  42.   '15': 'fifteen',
  43.   '16': 'sixteen',
  44.   '17': 'seventeen',
  45.   '18': 'eighteen',
  46.   '19': 'nineteen',
  47. };
  48.  
  49. if (len == 3) {
  50.   if (numberStr[2] === '0' && numberStr[1] === '0') {
  51.     p(`${unitsObj[numberStr[0]]} hundred`);
  52.   } else if (numberStr[1] === '0') {
  53.     p(`${unitsObj[numberStr[0]]} hundred and ${unitsObj[numberStr[2]]}`);
  54.   } else if (numberStr[2] === '0') {
  55.     if (numberStr[1] === '1') {
  56.       p(`${unitsObj[numberStr[0]]} hundred and ten`);
  57.     } else {
  58.       p(`${unitsObj[numberStr[0]]} hundred and ${dozensObj[numberStr[1] + '0']}`);
  59.     }
  60.   } else {
  61.     if (numberStr[1] === '1') {
  62.       p(`${unitsObj[numberStr[0]]} hundred and ${numbers10to19['1' + numberStr[2].toString()]}`);
  63.     } else {
  64.       p(`${unitsObj[numberStr[0]]} hundred and ${dozensObj[numberStr[1] +
  65.         '0']} ${unitsObj[numberStr[2]]}`);
  66.     }
  67.   }
  68. } else if (len === 2) {
  69.   if (numberStr[1] === '0') {
  70.     p(`${dozensObj[numberStr[0] + '0']}`);
  71.   } else if (numberStr[0] === '1') {
  72.     p(`${numbers10to19['1' + numberStr[1].toString()]}`);
  73.   } else {
  74.     p(`${dozensObj[numberStr[0] + '0']} ${unitsObj[numberStr[1]]}`);
  75.   }
  76. } else {
  77.   p(`${unitsObj[numberStr[0]]}`);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement