// I went ahead and added support for larger numbers over 999. // Uncomment lines below to get automatic support of bigger numbers: const bigNumbers = [ "", // "thousand", // "million", // "billion", // "trillion", // "quadrillion" ]; function Generrate(number) { // the only time we pronounce zero is when the number is zero itself if (number === 0) return "zero"; let result = ""; if (number < 0) { result += "negative "; number *= -1; } const thousandMultiples = []; let thousands = number; while (thousands) { thousandMultiples.push(thousands % 1000); thousands = parseInt(thousands / 1000); } if (thousandMultiples.length > bigNumbers.length) { return "unsupported number"; // return "Sorry, can't pronounce. The number is too big"; } const wordsArray = thousandMultiples.map( (elem, ind) => pronounceHundreds(elem) + " " + bigNumbers[ind] ); return (result + wordsArray.reverse().reduce((acc, cur) => acc + " " + cur)).trim(); } function pronounceHundreds(i) { const hundreds = parseInt(i / 100); const tens = i % 100; let result = pronounceOnes(hundreds); if (hundreds) { result += " hundred"; } // if number is not a pure hundred, add space and tens if (tens) { if (hundreds) { result += " and "; // added 'and' to pass the test. however not needed in American. // result += " "; } result += pronounceTens(tens); } return result; } function pronounceTens(i) { const tens = parseInt(i / 10); const ones = i % 10; let result = ""; const tensWords = [ "", pronounceTeens(ones), "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" ]; result += tensWords[tens]; // numbers 21-99 need to be hyphenated if (ones && tens > 1) { result += "-"; } if (ones && tens != 1) { result += pronounceOnes(ones); } return result; } function pronounceTeens(i) { return [ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ][i]; } function pronounceOnes(i) { return [ "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ][i]; }
function Generrate (n){- // I went ahead and added support for larger numbers over 999.
- // Uncomment lines below to get automatic support of bigger numbers:
var out = "";var str_arrOfDigits = n.toString().split("");if (str_arrOfDigits[0] == "-"){out += "negative ";str_arrOfDigits.splice(0,1);}switch (str_arrOfDigits.length) {case 1:out += Digits(str_arrOfDigits[0]);break;case 2:out += getDoubleDigit(str_arrOfDigits);break;case 3:out += getTrippleDigit(str_arrOfDigits)break;default:return "unsupported number";break;}return out;};function Digits(n) {return ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"][n];};function tens(n) {return ["zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"][n];};function getDoubleDigit (arr){var out = "";if (arr[0] == "1") {out = Digits(arr.join().replace("," ,""));}else {if (arr[0] != "0")out = tens(arr[0]) + (arr[1] != "0"? " " + Digits(arr[1]): "");elseout = Digits(arr[1]);}return out;- const bigNumbers = [
- "",
- // "thousand",
- // "million",
- // "billion",
- // "trillion",
- // "quadrillion"
- ];
- function Generrate(number) {
- // the only time we pronounce zero is when the number is zero itself
- if (number === 0) return "zero";
- let result = "";
- if (number < 0) {
- result += "negative ";
- number *= -1;
- }
- const thousandMultiples = [];
- let thousands = number;
- while (thousands) {
- thousandMultiples.push(thousands % 1000);
- thousands = parseInt(thousands / 1000);
- }
- if (thousandMultiples.length > bigNumbers.length) {
- return "unsupported number";
- // return "Sorry, can't pronounce. The number is too big";
- }
- const wordsArray = thousandMultiples.map(
- (elem, ind) => pronounceHundreds(elem) + " " + bigNumbers[ind]
- );
- return (result + wordsArray.reverse().reduce((acc, cur) => acc + " " + cur)).trim();
- }
function getTrippleDigit (arr){var firstDigit = Digits(arr[0]);var doubleDigits = getDoubleDigit(arr.splice(1));return (firstDigit != "zero"? firstDigit + " hundred" : "") + (firstDigit != "zero" && doubleDigits != "zero"? " and " + doubleDigits : (doubleDigits != "zero"? doubleDigits : ""));}- function pronounceHundreds(i) {
- const hundreds = parseInt(i / 100);
- const tens = i % 100;
- let result = pronounceOnes(hundreds);
- if (hundreds) {
- result += " hundred";
- }
- // if number is not a pure hundred, add space and tens
- if (tens) {
- if (hundreds) {
- result += " and "; // added 'and' to pass the test. however not needed in American.
- // result += " ";
- }
- result += pronounceTens(tens);
- }
- return result;
- }
- function pronounceTens(i) {
- const tens = parseInt(i / 10);
- const ones = i % 10;
- let result = "";
- const tensWords = [
- "",
- pronounceTeens(ones),
- "twenty",
- "thirty",
- "fourty",
- "fifty",
- "sixty",
- "seventy",
- "eighty",
- "ninety"
- ];
- result += tensWords[tens];
- // numbers 21-99 need to be hyphenated
- if (ones && tens > 1) {
- result += "-";
- }
- if (ones && tens != 1) {
- result += pronounceOnes(ones);
- }
- return result;
- }
- function pronounceTeens(i) {
- return [
- "ten",
- "eleven",
- "twelve",
- "thirteen",
- "fourteen",
- "fifteen",
- "sixteen",
- "seventeen",
- "eighteen",
- "nineteen"
- ][i];
- }
- function pronounceOnes(i) {
- return [
- "",
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine"
- ][i];
- }