Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function parseInt(string) {
- let first = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
- 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen',
- 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] // записываю промежуток от 0 до 19
- let second = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
- let array = []
- array = string.split(' ').join('-').split('-') // делаем массив только из слов
- for (let i = array.length - 1; i >= 0; i--) {
- if (array[i] == 'and') {
- array.splice(i, 1)
- }
- } // удаляем лишние 'and'
- if (array.length == 1) {
- for (let i = 0; i < first.length; i++) {
- if (array[0] == first[i]) {
- return i
- }
- }
- for (let i = 0; i < second.length; i++) {
- if (array[0] == second[i]) {
- return (i+2) * 10
- }
- }
- } // если передано одно слово
- else {
- let number = 0
- for (let i = 0; i < array.length; i++) {
- if (array[i] == 'million') {
- return number += 1000000 * first.indexOf(array[i - 1])
- }
- }
- let thousand = array.indexOf('thousand')
- if (thousand >= 0) {
- let firstChart = 0
- let secondChart = 0
- for (let i = 0; i < thousand; i++) {
- if (array[i] == 'hundred') {
- firstChart += 100 * first.indexOf(array[i-1])
- }
- if (second.indexOf(array[i]) >= 0) {
- firstChart += (second.indexOf(array[i]) + 2) * 10
- }
- if (first.indexOf(array[i]) >= 0 && (i != 0 || thousand == 1)) {
- firstChart += first.indexOf(array[i])
- }
- } // Первая часть числа
- for (let i = thousand + 1; i < array.length; i++) {
- if (array[i] == 'hundred') {
- secondChart += 100 * first.indexOf(array[i-1])
- }
- if (second.indexOf(array[i]) >= 0) {
- secondChart += (second.indexOf(array[i]) + 2) * 10
- }
- if (first.indexOf(array[i]) >= 0 && (i != thousand + 1 || array[i+1] != 'hundred')) {
- secondChart += first.indexOf(array[i])
- }
- } // Вторая часть числа
- number = firstChart * 1000 + secondChart
- } // Если есть тысячи
- else {
- for (let i = 0; i < array.length; i++) {
- if (array[i] == 'hundred') {
- number += 100 * first.indexOf(array[i-1])
- }
- if (second.indexOf(array[i]) >= 0) {
- number += (second.indexOf(array[i]) + 2) * 10
- }
- if (first.indexOf(array[i]) >= 0 && (i != 0)) {
- number += first.indexOf(array[i])
- }
- }
- } // Если тысяч нечу
- return number
- }
- }
- // ["seven", "hundred", "eighty", "three", "thousand", "nine", "hundred", "nineteen"]
- console.log(parseInt('seven hundred eighty-three thousand nine hundred and nineteen')) // rabotaet
- console.log(parseInt('nine hundred twenty one')) // rabotaet
- console.log(parseInt('one million')) // rabotaet
- console.log(parseInt('nine thousand nine hundred and nineteen')) // rabotaet
- console.log(parseInt('two thousand'))
- console.log(parseInt('seven hundred eighty-three thousand'))
- console.log(parseInt('eighty three thousand'))
- console.log(parseInt('eighty thousand'))
- console.log(parseInt('two hundred thousand three'))
- console.log(parseInt('eighty three'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement