Created
June 11, 2020 15:52
-
-
Save Gosilama/4ee24b4b28c08929db3b82a338eb0ef4 to your computer and use it in GitHub Desktop.
Write a function that converts number to text, Passing an int number should return array of text number eg 23498643 => [two, three, four, nine, eight, six, four, three] 40263645 => [four, zero, two, six, three, six, four, four, five] You5:43 PM Update the function to group the numbers by 2s 23498643 => [twenty-three, fourty-nine, eighty-six, fou…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ds2 = { | |
unit: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], | |
tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] | |
}; | |
const isUnit = (num) => num % 10 === parseInt(num); | |
const isTeen = (num) => num[0] === '1' && num.length > 1; | |
const isTens = (num) => num[0] > 1 && num.length > 1; | |
const getNumStrArr2 = (num) => { | |
const parts = num.toString().match(/.{1,2}/g); | |
const resultArr = []; | |
parts.forEach((part) => { | |
const m = parseInt(part); | |
if (isTeen(part) || isUnit(part)) { | |
resultArr.push(ds2.unit[m]); | |
} | |
if (isTens(part)) { | |
resultArr.push(`${ds2.tens[parseInt(part[0]) - 2]}${part[1] !== '0' ? '-' + ds2.unit[parseInt(part[1])] : ''}`); | |
} | |
}); | |
return resultArr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment