-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromannumbers.js
136 lines (123 loc) · 4.04 KB
/
romannumbers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Roman numbers converter
*
* written by Doğukan Öksüz
* www.dogukanoksuz.com.tr
* github.com/dogukanoksuz
*
*/
"use strict";
class RomanNumbers {
constructor() {
// init numbers class with needed numbers/symbols
this.numbers = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}
};
convertToNumber = (input) => {
// function name is explaining everything
let result = 0;
// uppercase the input.
input = input.toUpperCase();
// ES6 type parse for chars
let arr = [...input];
for (let i = 0; i < arr.length; i++) {
if (i + 1 < arr.length) {
if (this.numbers[arr[i]] >= this.numbers[arr[i + 1]]) {
result += this.numbers[arr[i]];
} else {
result += this.numbers[arr[i + 1]] - this.numbers[arr[i]];
i++;
}
} else {
result += this.numbers[arr[i]];
i++;
}
}
return result;
};
convertToRomanNumber = (input) => {
let result = "";
input = Number(input);
let addDigit = (inputChar, Times) => {
let a = "";
Times = Math.floor(Times);
if (Times === 1) {
return inputChar;
} else {
for (let i = 0; i < Times; i++) {
a += inputChar;
}
return a;
}
};
while (input !== 0) {
if (input >= 1000) {
result += addDigit("M", input / 1000);
input %= 1000;
} else if (input >= 500) {
if (input < 900) {
result += addDigit("D", input / 500);
input %= 500;
} else {
result += addDigit("CM", 1);
input %= 100;
}
} else if (input >= 100) {
if (input < 400) {
result += addDigit("C", input / 100);
input %= 100;
} else {
result += addDigit("CD", 1);
input %= 100;
}
} else if (input >= 50) {
if (input < 90) {
result += addDigit("L", input / 50);
input %= 50;
} else {
result += addDigit("XC", 1);
input %= 10;
}
} else if (input >= 10) {
if (input < 40) {
result += addDigit("X", input / 10);
input %= 10;
} else {
result += addDigit("XL", 1);
input %= 10;
}
} else if (input >= 5) {
if (input < 9) {
result += addDigit("V", input / 5);
input %= 5;
} else {
result += addDigit("IX", 1);
input = 0;
}
} else if (input >= 1) {
if (input < 4) {
result += addDigit("I", input);
input = 0;
} else {
result += addDigit("IV", 1);
input = 0;
}
}
}
return result;
};
}
let _RomanNumbers = new RomanNumbers();
let RomanToDecimal = () => {
document.getElementById("output_1").innerText = isNaN(_RomanNumbers.convertToNumber(document.getElementById("input_1").value)) ? "i think you write a wrong thing." : _RomanNumbers.convertToNumber(document.getElementById("input_1").value);
};
let DecimalToRoman = () => {
document.getElementById("output_2").innerText = !isNaN(_RomanNumbers.convertToRomanNumber(document.getElementById("input_2").value)) ? "i think you write a wrong thing." : _RomanNumbers.convertToRomanNumber(document.getElementById("input_2").value);
};