-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
33 lines (25 loc) · 839 Bytes
/
script.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
function convertToRoman(num) {
let decimals = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
let romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V",
"IV", "I"];
let romanNum = ""; //"X"+"X"+"X"+"V"+"I" -> "XXXVI"
for(let i = 0; i <= decimals.length; i++) {
while (decimals[i] <= num) {
romanNum += romans[i];
num -= decimals[i];
}
}
return romanNum;
}
let input = document.getElementById('quantity');
let output = document.getElementById('result');
input.onkeyup = (function(e) {
e.preventDefault();
let number = input.value;
if (number <= 3999) {
output.textContent = convertToRoman(number);
}
else {
output.innerText = 'Please, enter a number less than 3999';
}
});