-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0273-integer-to-english-words.js
More file actions
63 lines (55 loc) · 2.36 KB
/
0273-integer-to-english-words.js
File metadata and controls
63 lines (55 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Integer To English Words
* Time Complexity: O(1)
* Space Complexity: O(1)
*/
var numberToWords = function (initialNumber) {
if (initialNumber === 0) {
return 'Zero';
}
const singleDigitWords = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
const teenWords = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
const tensWords = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
const magnitudeWords = ['', 'Thousand', 'Million', 'Billion'];
const convertThreeDigits = (segmentValue) => {
let segmentStringRepresentation = '';
const hundredsAmount = Math.floor(segmentValue / 100);
const twoDigitRemainder = segmentValue % 100;
if (hundredsAmount > 0) {
segmentStringRepresentation += singleDigitWords[hundredsAmount] + ' Hundred';
}
if (twoDigitRemainder > 0) {
if (segmentStringRepresentation !== '') {
segmentStringRepresentation += ' ';
}
if (twoDigitRemainder < 10) {
segmentStringRepresentation += singleDigitWords[twoDigitRemainder];
} else if (twoDigitRemainder < 20) {
segmentStringRepresentation += teenWords[twoDigitRemainder - 10];
} else {
segmentStringRepresentation += tensWords[Math.floor(twoDigitRemainder / 10)];
const unitNumber = twoDigitRemainder % 10;
if (unitNumber > 0) {
segmentStringRepresentation += ' ' + singleDigitWords[unitNumber];
}
}
}
return segmentStringRepresentation;
};
let workingNumber = initialNumber;
let magnitudePosition = 0;
const constructedParts = [];
while (workingNumber > 0) {
const currentThreeDigits = workingNumber % 1000;
if (currentThreeDigits > 0) {
let localizedWords = convertThreeDigits(currentThreeDigits);
if (magnitudePosition > 0) {
localizedWords += ' ' + magnitudeWords[magnitudePosition];
}
constructedParts.unshift(localizedWords);
}
workingNumber = Math.floor(workingNumber / 1000);
magnitudePosition++;
}
return constructedParts.join(' ').trim();
};