-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (93 loc) · 2.41 KB
/
app.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
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
const decimalValue = [100000, 90000, 50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
const romanNumeral = [
'\u2188',
'\u2182\u2188',
'\u2187',
'\u2182\u2187',
'\u2182',
'M\u2182',
'\u2181',
'M\u2181',
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I",
];
function romanify(){
let arabicInput = document.getElementById('arabicInput').value;
let result = parseInt(arabicInput);
if (!isNaN(result)) {
document.getElementById('romanInput').value = romanConvert(result);
$('#errorMsg').text('');
}
if(!(arabicInput.match(/^[0-9]+$/g))|| result > 399999){
document.getElementById('romanInput').value = '';
$('#errorMsg').text('Enter a number from 1 to 399999');
$('#arabicInput').addClass('is-invalid');
}
if(arabicInput.length === 0){
$('#errorMsg').text('');
$('#arabicInput').removeClass('is-invalid');
}
}
function numerify(){
let romanInput = document.getElementById('romanInput').value;
let result = romanInput;
if (!isNaN(result) || arabicConvert(result) == 0) {
document.getElementById('arabicInput').value = '';
$('#romanErrorMsg').text('Enter a valid Roman numeral in all caps');
$('#romanInput').addClass('is-invalid');
}
else{
document.getElementById('arabicInput').value = arabicConvert(result);
$('#romanErrorMsg').text('');
}
if(romanInput.length === 0){
$('#romanErrorMsg').text('');
$('#romanInput').removeClass('is-invalid');
}
}
function arabicConvert(str){
if(str.match(/[a-zABE-HJ-UWYZ]/g)){
return 0;
}
let numeral = 0;
for (let i = decimalValue.length-1; i > 0; i--) {
console.log(i)
while (str.includes(romanNumeral[i])) {
let matchIndex = str.indexOf(romanNumeral[i]);
let x = str[matchIndex] + str[matchIndex+1];
console.log(x);
if(romanNumeral.includes(x)){
numeral+= decimalValue[romanNumeral.indexOf(x)]
str = str.replace(x, "")
}
else{
numeral+= decimalValue[i];
str = str.replace(romanNumeral[i], "")
}
console.log(str, numeral);
console.log(str, numeral);
}
}
return numeral;
}
function romanConvert(num){
let romanized = "";
for (let i = 0; i < decimalValue.length; i++) {
while (decimalValue[i] <= num) {
romanized += romanNumeral[i];
num -= decimalValue[i];
}
}
return romanized;
}