-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0012-integer-to-roman.js
More file actions
83 lines (81 loc) · 1.63 KB
/
0012-integer-to-roman.js
File metadata and controls
83 lines (81 loc) · 1.63 KB
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
/**
* https://leetcode.com/problems/integer-to-roman/
* @param {number} num
* @return {string}
*/
var intToRoman = function (num) {
let ans = '';
while (num !== 0) {
//M == 1000
if (num >= 1000) {
num -= 1000;
ans += 'M';
}
//CM == 900
else if (num >= 900) {
num -= 900;
ans += 'CM';
}
//D == 500
else if (num >= 500) {
num -= 500;
ans += 'D';
}
//CD == 400
else if (num >= 400) {
num -= 400;
ans += 'CD';
}
//C == 100
else if (num >= 100) {
num -= 100;
ans += 'C';
}
//XC == 90
else if (num >= 90) {
num -= 90;
ans += 'XC';
}
//L == 50;
else if (num >= 50) {
num -= 50;
ans += 'L';
}
//XL == 40
else if (num >= 40) {
num -= 40;
ans += 'XL';
}
//X == 10
else if (num >= 10) {
num -= 10;
ans += 'X';
}
//IX == 9
else if (num >= 9) {
num -= 9;
ans += 'IX';
}
//V == 5
else if (num >= 5) {
num -= 5;
ans += 'V';
}
//IV == 4
else if (num >= 4) {
num -= 4;
ans += 'IV';
}
//II == 2
else if (num >= 2) {
num -= 2;
ans += 'II';
}
//I == 1
else {
num -= 1;
ans += 'I';
}
}
return ans;
};