-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.js
46 lines (41 loc) · 1.51 KB
/
roman.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
function romanNumeral(decimalNumber) {
var romanNumeral = "";
var romanNumerals = ["I", "V", "X", "L", "C", "D", "M"];
var index = 0;
try {
if (Number.isInteger(decimalNumber)) throw "Not an integer";
if ((decimalNumber <= 0) || (decimalNumber >= 4000)) throw "Out of range";
}
catch(err) {
var errContent= document.getElementsByClassName("error")[0]
errContent.innerHTML = err;
errContent.style.display = "block";
setTimeout(function(){ errContent.style.display = "none"; }, 2000);
return " "
}
// verifier que c'est bien un nombre et dans l'intervalle [1-3999]
while(decimalNumber > 0) {
var digit = decimalNumber % 10;
if (digit < 4) {
romanNumeral = romanNumerals[index].repeat(digit) + romanNumeral;
}
else if (digit == 4) {
romanNumeral = romanNumerals[index] + romanNumerals[index + 1] + romanNumeral;
}
else if (digit == 9) {
romanNumeral = romanNumerals[index] + romanNumerals[index + 2] + romanNumeral;
}
else {
romanNumeral = romanNumerals[index + 1] + romanNumerals[index].repeat(digit - 5) + romanNumeral;
}
decimalNumber = Math.floor(decimalNumber / 10);
index += 2;
}
return romanNumeral;
}
function showRoman(decimalNumber, id) {
var result = romanNumeral(decimalNumber);
if (result) {
document.getElementsByClassName(id)[0].innerHTML = result;
}
}