-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconverterscript.js
More file actions
138 lines (113 loc) · 4.54 KB
/
converterscript.js
File metadata and controls
138 lines (113 loc) · 4.54 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const numberToWordsInternational = (num) => {
const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
const scales = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'];
if (num === 0) return 'zero';
let words = '';
const getChunkWords = (chunk) => {
let chunkWords = '';
if (chunk >= 100) {
chunkWords += ones[Math.floor(chunk / 100)] + ' hundred ';
chunk %= 100;
}
if (chunk >= 10 && chunk < 20) {
chunkWords += teens[chunk - 10] + ' ';
} else if (chunk >= 20) {
chunkWords += tens[Math.floor(chunk / 10)] + ' ';
chunk %= 10;
}
if (chunk > 0) {
chunkWords += ones[chunk] + ' ';
}
return chunkWords.trim();
};
let scaleIndex = 0;
while (num > 0) {
let chunk = num % 1000;
if (chunk > 0) {
words = getChunkWords(chunk) + ' ' + scales[scaleIndex] + ' ' + words;
}
num = Math.floor(num / 1000);
scaleIndex++;
}
return words.trim();
};
const numberToWordsIndian = (num) => {
const ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
const tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
const scales = ['', 'thousand', 'lakh', 'crore','arab', 'kharab', 'neel', 'padma', 'shankh'];
if (num === 0) return 'zero';
const getChunkWords = (chunk) => {
let chunkWords = '';
if (chunk >= 100) {
chunkWords += ones[Math.floor(chunk / 100)] + ' hundred ';
chunk %= 100;
}
if (chunk >= 10 && chunk < 20) {
chunkWords += teens[chunk - 10] + ' ';
} else if (chunk >= 20) {
chunkWords += tens[Math.floor(chunk / 10)] + ' ';
chunk %= 10;
}
if (chunk > 0) {
chunkWords += ones[chunk] + ' ';
}
return chunkWords.trim();
};
let words = '';
let scaleIndex = 0;
const groups = [];
if (num > 999) {
groups.push(num % 1000);
num = Math.floor(num / 1000);
}
while (num > 0) {
groups.push(num % 100);
num = Math.floor(num / 100);
}
groups.reverse().forEach((chunk, index) => {
if (chunk > 0) {
words += getChunkWords(chunk) + ' ' + scales[groups.length - 1 - index] + ' ';
}
});
return words.trim();
};
const validateAndCleanNumberInput = (input) => {
const containsCommas = input.includes(',');
if (containsCommas) {
const regex = /^\d{1,3}(,\d{2,3})*$/;
if (!regex.test(input)) {
return { valid: false, number: null };
}
input = input.replace(/,/g, '');
}
if (isNaN(input)) {
return { valid: false, number: null };
}
return { valid: true, number: parseInt(input, 10) };
};
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('convertButton').addEventListener('click', () => {
const numberInput = document.getElementById('numberInput').value.trim();
const numberingSystem = document.getElementById('numberingSystem').value;
const result = document.getElementById('result');
if (numberInput === '') {
result.textContent = 'Please enter a number!';
return;
}
const { valid, number } = validateAndCleanNumberInput(numberInput);
if (!valid) {
result.textContent = 'Invalid number format! Please enter a number in the correct format.';
return;
}
let numberWords = '';
if (numberingSystem === 'international') {
numberWords = numberToWordsInternational(number);
} else if (numberingSystem === 'indian') {
numberWords = numberToWordsIndian(number);
}
result.textContent = `Converted Words: ${numberWords}`;
});
});