Advertisement
sheredega

JS - code3

Dec 13th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function parseInt(string) {
  2.     let first = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
  3.         'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
  4.         'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] // записываю промежуток от 0 до 19
  5.     let second = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
  6.     let array = []
  7.  
  8.     array = string.split(' ').join('-').split('-') // делаем массив только из слов
  9.  
  10.     for (let i = array.length - 1; i >= 0; i--) {
  11.         if (array[i] == 'and') {
  12.             array.splice(i, 1)
  13.         }
  14.     } // удаляем лишние 'and'
  15.  
  16.     if (array.length == 1) {
  17.         for (let i = 0; i < first.length; i++) {
  18.             if (array[0] == first[i]) {
  19.                 return i
  20.             }
  21.         }
  22.         for (let i = 0; i < second.length; i++) {
  23.             if (array[0] == second[i]) {
  24.                 return (i+2) * 10
  25.             }
  26.         }
  27.     } // если передано одно слово
  28.  
  29.     else {
  30.         let number = 0
  31.         for (let i = 0; i < array.length; i++) {
  32.             if (array[i] == 'million') {
  33.                 return number += 1000000 * first.indexOf(array[i - 1])
  34.             }
  35.         }
  36.         let thousand = array.indexOf('thousand')
  37.         if (thousand >= 0) {
  38.             let firstChart = 0
  39.             let secondChart = 0
  40.                 for (let i = 0; i < thousand; i++) {
  41.                     if (array[i] == 'hundred') {
  42.                         firstChart += 100 * first.indexOf(array[i-1])
  43.                     }
  44.                     if (second.indexOf(array[i]) >= 0) {
  45.                         firstChart += (second.indexOf(array[i]) + 2) * 10
  46.                     }
  47.                     if (first.indexOf(array[i]) >= 0 && (i != 0 || thousand == 1)) {
  48.                         firstChart += first.indexOf(array[i])
  49.                     }
  50.                 } // Первая часть числа
  51.                 for (let i = thousand + 1; i < array.length; i++) {
  52.                     if (array[i] == 'hundred') {
  53.                         secondChart += 100 * first.indexOf(array[i-1])
  54.                     }
  55.                     if (second.indexOf(array[i]) >= 0) {
  56.                         secondChart += (second.indexOf(array[i]) + 2) * 10
  57.                     }
  58.                     if (first.indexOf(array[i]) >= 0 && (i != thousand + 1 || array[i+1] != 'hundred')) {
  59.                         secondChart += first.indexOf(array[i])
  60.                     }
  61.                 } // Вторая часть числа
  62.                 number = firstChart * 1000 + secondChart
  63.  
  64.  
  65.         } // Если есть тысячи
  66.         else {
  67.             for (let i = 0; i < array.length; i++) {
  68.                 if (array[i] == 'hundred') {
  69.                     number += 100 * first.indexOf(array[i-1])
  70.                 }
  71.                 if (second.indexOf(array[i]) >= 0) {
  72.                     number += (second.indexOf(array[i]) + 2) * 10
  73.                 }
  74.                 if (first.indexOf(array[i]) >= 0 && (i != 0)) {
  75.                     number += first.indexOf(array[i])
  76.                 }
  77.             }
  78.         } // Если тысяч нечу
  79.         return number
  80.     }
  81. }
  82. // ["seven", "hundred", "eighty", "three", "thousand", "nine", "hundred", "nineteen"]
  83. console.log(parseInt('seven hundred eighty-three thousand nine hundred and nineteen')) // rabotaet
  84. console.log(parseInt('nine hundred twenty one')) // rabotaet
  85. console.log(parseInt('one million')) // rabotaet
  86. console.log(parseInt('nine thousand nine hundred and nineteen')) // rabotaet
  87. console.log(parseInt('two thousand'))
  88. console.log(parseInt('seven hundred eighty-three thousand'))
  89. console.log(parseInt('eighty three thousand'))
  90. console.log(parseInt('eighty thousand'))
  91. console.log(parseInt('two hundred thousand three'))
  92. console.log(parseInt('eighty three'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement