Skip to content

Instantly share code, notes, and snippets.

@Gosilama
Created June 11, 2020 15:52
Show Gist options
  • Save Gosilama/4ee24b4b28c08929db3b82a338eb0ef4 to your computer and use it in GitHub Desktop.
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…
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