-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
77 lines (63 loc) · 1.57 KB
/
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
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
const form = document.getElementById('form');
const convertButton = document.getElementById('convert-btn');
const output = document.getElementById('output');
const conversion = num => {
const index = [
['M', 1000],
['CM', 900],
['D', 500],
['CD', 400],
['C', 100],
['XC', 90],
['L', 50],
['XL', 40],
['X', 10],
['IX', 9],
['V', 5],
['IV', 4],
['I', 1]
];
const myResult = [];
index.forEach(function (arr) {
while (num >= arr[1]) {
myResult.push(arr[0]);
num -= arr[1];
}
});
return myResult.join('');
};
const validate = (str, int) => {
let errorTxt = '';
if (!str || str.match(/[e.]/g)) {
errorTxt = 'Please enter a valid number!';
} else if (int < 1) {
errorTxt = 'Please enter a number greater than or equal to 1!';
} else if (int > 3999) {
errorTxt = 'Please enter a number less than or equal to 3999!';
} else {
return true;
}
output.innerText = errorTxt;
output.classList.add('alert');
return false;
};
const clearOutput = () => {
output.innerText = '';
output.classList.remove('alert');
};
form.addEventListener('submit', e => {
e.preventDefault();
doUpdate();
});
convertButton.addEventListener('click', () => {
doUpdate();
});
const doUpdate = () => {
const numStr = document.getElementById('number').value;
const int = parseInt(numStr, 10);
output.classList.remove('hidden');
clearOutput();
if (validate(numStr, int)) {
output.innerText = conversion(int);
}
};