Skip to content

Instantly share code, notes, and snippets.

@GarrettS
Last active April 10, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarrettS/72fbd1e53644d27c44aaa258e2a0992a to your computer and use it in GitHub Desktop.
Save GarrettS/72fbd1e53644d27c44aaa258e2a0992a to your computer and use it in GitHub Desktop.
Text Dollar — JP Morgan Chase Interview Question // source https://jsbin.com/tinuqar
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="JP Morgan Chase Interview Question">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JP Morgan Chase Intervew Question</title>
</head>
<body>
<h1></h1>
<script id="jsbin-javascript">
/**
Text Dollar
Difficulty: Hard
Time Limit: 45 min
Description
You are given a positive integer number. This represents the sales made that day in your
department store. The payables department however, needs this printed out in English. NOTE:
The correct spelling of 40 is Forty. (NOT Fourty)
Input
Your program should read lines of text from standard input. Each line contains a positive integer.
Output
For each set of input print a single line to standard output which is the english textual
representation of that integer. The output should be unspaced and in CamelCased. Always
assume plural quantities. You can also assume that the numbers are &lt; 1000000000 (1 billion). In
case of ambiguities eg. 2200 could be TwoThousandTwoHundredDollars or
TwentyTwoHundredDollars, always choose the representation with the larger base i.e.
TwoThousandTwoHundredDollars.
Test 1
Input
3
Expected Output
ThreeDollars
Test 2
Input
466
Expected Output
FourHundredSixtySixDollars
Test 3
Input
1234
Expected Output
OneThousandTwoHundredThirtyFourDollars
Test 4
Input
10
Expected Output
TenDollars
Test 5
Input
21
Expected Output
TwentyOneDollars
*/
function numberToWords(input) {
let inputArray = String(input).split("\n");
let outputArray = new Array(inputArray.length);
for(let i = 0; i < inputArray.length; i++) {
outputArray[i] = numberToWordsSub(inputArray[i]);
}
return outputArray.join("\n");
function numberToWordsSub(n) {
if(+n == 0) {
return"ZeroDollars";
}
if(n >= 1e9 || n < 0) {
throw new RangeError("Error: '" + n + "'. number must be < 1,000,000,000");
}
if(!+n) {
throw new TypeError("Error: '" + n + "'. Input `n` must be: 0 <= n < 1,000,000,000");
}
let tupletArray = (+n).toLocaleString().split(",").reverse();
let WORD_BANK = getWordBank(),
TENS = WORD_BANK.TENS,
ONES = WORD_BANK.ONES;
let hundredWord = formatHundreds(tupletArray[0]);
let thousandWord = formatHundreds(tupletArray[1], "Thousand");
let millionWord = formatHundreds(tupletArray[2], "Million");
let dollarWord = n == 1 ? "Dollar" : "Dollars";
return millionWord + thousandWord + hundredWord + dollarWord;
function formatHundreds(tupletArray, hundredsSuffixer) {
if(!tupletArray) return"";
hundredsSuffixer = hundredsSuffixer || "";
var twentyPart = tupletArray.match(/([2-9])(?:[0-9])$/);
twentyPart = twentyPart && twentyPart[1];
var nineteenPart;
var onesPart = "";
var hundredsPart = "";
var resultWord = "";
tensWord = TENS[twentyPart];
if(tensWord) {
onesPart = tupletArray.match(/(\d)$/);
onesPart = onesPart && onesPart[1];
tensWord += ONES[onesPart];
} else {
nineteenPart = tupletArray.match(/(?:(?:1\d)|(?:[1-9]))$/);
nineteenPart = nineteenPart && nineteenPart[0]
tensWord = ONES[nineteenPart];
}
hundredsPart = tupletArray.match(/(\d{1})(?:\d{2})$/);
hundredsPart = hundredsPart && hundredsPart[1];
if(hundredsPart in ONES) {
resultWord += ONES[hundredsPart] + "Hundred";
}
if(tensWord) {
resultWord += tensWord;
} else if(tensPart in TENS) {
resultWord += TENS[tensPart] + ONES[onesPart];
}
return resultWord && resultWord + hundredsSuffixer || "";
}
function getWordBank() {
let ONES = {
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"
};
let TENS = {
"2": "Twenty",
"3": "Thirty",
"4": "Förty",
"5": "Fifty",
"6": "Sixty",
"7": "Seventy",
"8": "Eighty",
"9": "Ninety"
};
return {ONES: ONES, TENS: TENS};
}
}
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
Text Dollar
Difficulty: Hard
Time Limit: 45 min
Description
You are given a positive integer number. This represents the sales made that day in your
department store. The payables department however, needs this printed out in English. NOTE:
The correct spelling of 40 is Forty. (NOT Fourty)
Input
Your program should read lines of text from standard input. Each line contains a positive integer.
Output
For each set of input print a single line to standard output which is the english textual
representation of that integer. The output should be unspaced and in CamelCased. Always
assume plural quantities. You can also assume that the numbers are &lt; 1000000000 (1 billion). In
case of ambiguities eg. 2200 could be TwoThousandTwoHundredDollars or
TwentyTwoHundredDollars, always choose the representation with the larger base i.e.
TwoThousandTwoHundredDollars.
Test 1
Input
3
Expected Output
ThreeDollars
Test 2
Input
466
Expected Output
FourHundredSixtySixDollars
Test 3
Input
1234
Expected Output
OneThousandTwoHundredThirtyFourDollars
Test 4
Input
10
Expected Output
TenDollars
Test 5
Input
21
Expected Output
TwentyOneDollars
*/
function numberToWords(input) {
let inputArray = String(input).split("\n");
let outputArray = new Array(inputArray.length);
for(let i = 0; i < inputArray.length; i++) {
outputArray[i] = numberToWordsSub(inputArray[i]);
}
return outputArray.join("\n");
function numberToWordsSub(n) {
if(+n == 0) {
return"ZeroDollars";
}
if(n >= 1e9 || n < 0) {
throw new RangeError("Error: '" + n + "'. number must be < 1,000,000,000");
}
if(!+n) {
throw new TypeError("Error: '" + n + "'. Input `n` must be: 0 <= n < 1,000,000,000");
}
let tupletArray = (+n).toLocaleString().split(",").reverse();
let WORD_BANK = getWordBank(),
TENS = WORD_BANK.TENS,
ONES = WORD_BANK.ONES;
let hundredWord = formatHundreds(tupletArray[0]);
let thousandWord = formatHundreds(tupletArray[1], "Thousand");
let millionWord = formatHundreds(tupletArray[2], "Million");
let dollarWord = n == 1 ? "Dollar" : "Dollars";
return millionWord + thousandWord + hundredWord + dollarWord;
function formatHundreds(tupletArray, hundredsSuffixer) {
if(!tupletArray) return"";
hundredsSuffixer = hundredsSuffixer || "";
var twentyPart = tupletArray.match(/([2-9])(?:[0-9])$/);
twentyPart = twentyPart && twentyPart[1];
var nineteenPart;
var onesPart = "";
var hundredsPart = "";
var resultWord = "";
tensWord = TENS[twentyPart];
if(tensWord) {
onesPart = tupletArray.match(/(\d)$/);
onesPart = onesPart && onesPart[1];
tensWord += ONES[onesPart];
} else {
nineteenPart = tupletArray.match(/(?:(?:1\d)|(?:[1-9]))$/);
nineteenPart = nineteenPart && nineteenPart[0]
tensWord = ONES[nineteenPart];
}
hundredsPart = tupletArray.match(/(\d{1})(?:\d{2})$/);
hundredsPart = hundredsPart && hundredsPart[1];
if(hundredsPart in ONES) {
resultWord += ONES[hundredsPart] + "Hundred";
}
if(tensWord) {
resultWord += tensWord;
} else if(tensPart in TENS) {
resultWord += TENS[tensPart] + ONES[onesPart];
}
return resultWord && resultWord + hundredsSuffixer || "";
}
function getWordBank() {
let ONES = {
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"
};
let TENS = {
"2": "Twenty",
"3": "Thirty",
"4": "Förty",
"5": "Fifty",
"6": "Sixty",
"7": "Seventy",
"8": "Eighty",
"9": "Ninety"
};
return {ONES: ONES, TENS: TENS};
}
}
}</script></body>
</html>
/**
Text Dollar
Difficulty: Hard
Time Limit: 45 min
Description
You are given a positive integer number. This represents the sales made that day in your
department store. The payables department however, needs this printed out in English. NOTE:
The correct spelling of 40 is Forty. (NOT Fourty)
Input
Your program should read lines of text from standard input. Each line contains a positive integer.
Output
For each set of input print a single line to standard output which is the english textual
representation of that integer. The output should be unspaced and in CamelCased. Always
assume plural quantities. You can also assume that the numbers are &lt; 1000000000 (1 billion). In
case of ambiguities eg. 2200 could be TwoThousandTwoHundredDollars or
TwentyTwoHundredDollars, always choose the representation with the larger base i.e.
TwoThousandTwoHundredDollars.
Test 1
Input
3
Expected Output
ThreeDollars
Test 2
Input
466
Expected Output
FourHundredSixtySixDollars
Test 3
Input
1234
Expected Output
OneThousandTwoHundredThirtyFourDollars
Test 4
Input
10
Expected Output
TenDollars
Test 5
Input
21
Expected Output
TwentyOneDollars
*/
function numberToWords(input) {
let inputArray = String(input).split("\n");
let outputArray = new Array(inputArray.length);
for(let i = 0; i < inputArray.length; i++) {
outputArray[i] = numberToWordsSub(inputArray[i]);
}
return outputArray.join("\n");
function numberToWordsSub(n) {
if(+n == 0) {
return"ZeroDollars";
}
if(n >= 1e9 || n < 0) {
throw new RangeError("Error: '" + n + "'. number must be < 1,000,000,000");
}
if(!+n) {
throw new TypeError("Error: '" + n + "'. Input `n` must be: 0 <= n < 1,000,000,000");
}
let tupletArray = (+n).toLocaleString().split(",").reverse();
let WORD_BANK = getWordBank(),
TENS = WORD_BANK.TENS,
ONES = WORD_BANK.ONES;
let hundredWord = formatHundreds(tupletArray[0]);
let thousandWord = formatHundreds(tupletArray[1], "Thousand");
let millionWord = formatHundreds(tupletArray[2], "Million");
let dollarWord = n == 1 ? "Dollar" : "Dollars";
return millionWord + thousandWord + hundredWord + dollarWord;
function formatHundreds(tupletArray, hundredsSuffixer) {
if(!tupletArray) return"";
hundredsSuffixer = hundredsSuffixer || "";
var twentyPart = tupletArray.match(/([2-9])(?:[0-9])$/);
twentyPart = twentyPart && twentyPart[1];
var nineteenPart;
var onesPart = "";
var hundredsPart = "";
var resultWord = "";
tensWord = TENS[twentyPart];
if(tensWord) {
onesPart = tupletArray.match(/(\d)$/);
onesPart = onesPart && onesPart[1];
tensWord += ONES[onesPart];
} else {
nineteenPart = tupletArray.match(/(?:(?:1\d)|(?:[1-9]))$/);
nineteenPart = nineteenPart && nineteenPart[0]
tensWord = ONES[nineteenPart];
}
hundredsPart = tupletArray.match(/(\d{1})(?:\d{2})$/);
hundredsPart = hundredsPart && hundredsPart[1];
if(hundredsPart in ONES) {
resultWord += ONES[hundredsPart] + "Hundred";
}
if(tensWord) {
resultWord += tensWord;
} else if(tensPart in TENS) {
resultWord += TENS[tensPart] + ONES[onesPart];
}
return resultWord && resultWord + hundredsSuffixer || "";
}
function getWordBank() {
let ONES = {
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"
};
let TENS = {
"2": "Twenty",
"3": "Thirty",
"4": "Förty",
"5": "Fifty",
"6": "Sixty",
"7": "Seventy",
"8": "Eighty",
"9": "Ninety"
};
return {ONES: ONES, TENS: TENS};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment