From ebbcee5f975e5a25089065293633111d83a1f5ca Mon Sep 17 00:00:00 2001 From: asselzhl Date: Sun, 15 Oct 2023 19:13:16 +0600 Subject: [PATCH] add solution --- package-lock.json | 2 +- src/index.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index f74716981..d5451a939 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "semver": "^6.3.0" }, "engines": { - "node": ">=12" + "node": ">=16" } }, "node_modules/ansi-colors": { diff --git a/src/index.js b/src/index.js index eeae09b1b..772c2918d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,60 @@ module.exports = function toReadable (number) { + const numbersTable = { + 0: 'zero', + 1: 'one', + 2: 'two', + 3: 'three', + 4: 'four', + 5: 'five', + 6: 'six', + 7: 'seven', + 8: 'eight', + 9: 'nine', + 10: 'ten', + 11: 'eleven', + 12: 'twelve', + 13: 'thirteen', + 14: 'fourteen', + 15: 'fifteen', + 16: 'sixteen', + 17: 'seventeen', + 18: 'eighteen', + 19: 'nineteen', + 20: 'twenty', + 30: 'thirty', + 40: 'forty', + 50: 'fifty', + 60: 'sixty', + 70: 'seventy', + 80: 'eighty', + 90: 'ninety', + 100: 'one hundred' + } -} + let result = ''; + let tens; + let units = number % 10; + + if (numbersTable[number]) { + return numbersTable[number]; + } else { + if (parseInt(number / 100)) { + let hundreds = parseInt(number / 100); + if (number % 100 == 0) { + result = numbersTable[hundreds] + ' hundred'; + } else { + tens = number - (number - (number % 100)); + if (numbersTable[tens]) { + result = numbersTable[hundreds] + ' hundred ' + numbersTable[tens]; + } else { + tens = tens - units; + result = numbersTable[hundreds] + ' hundred ' + numbersTable[tens] + ' ' + numbersTable[units]; + } + } + } else if (parseInt(number / 10)) { + tens = number - units; + result = numbersTable[tens] + ' ' + numbersTable[units]; + } + } + return result; + }