-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (53 loc) · 1.51 KB
/
main.js
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
function intToRoman(num) {
const symbolsList = [
["I", 1],
["IV", 4],
["V", 5],
["IX", 9],
["X", 10],
["XL", 40],
["L", 50],
["XC", 90],
["C", 100],
["CD", 400],
["D", 500],
["CM", 900],
["M", 1000],
['V\u0305', 5000],
['X\u0305', 10000],
['L\u0305', 50000],
['C\u0305', 100000],
['D\u0305', 500000],
['M\u0305', 1000000],
];
let i = symbolsList.length - 1;
let roman = "";
while (num > 0) {
const currentDivider = symbolsList[i][1];
const currentSymbol = symbolsList[i][0];
const result = Math.floor(num / currentDivider);
if (result > 0) {
let temp = "";
for (let j = 0; j < result; j++) {
temp += currentSymbol;
}
roman += temp;
num %= currentDivider;
}
i -= 1;
}
return roman;
}
// https://www.calculateme.com/roman-numerals/to-roman/
const submitNumber = document.getElementById("number");
const submitButton = document.getElementById("convert-button");
const resultBox = document.getElementById("error");
const interpretationBox = document.getElementById("output");
submitButton.addEventListener("click", function(){
const userInput = submitNumber.value;
if (userInput>0 && userInput<=4000000) {
interpretationBox.textContent=userInput +"="+ intToRoman(userInput)
} else {
interpretationBox.textContent="Please enter values that are between 1 and 4,000,000"
}
});