Skip to content

Commit ff1592f

Browse files
author
Liza Nguyen
committed
Roman numerals - work in progress
1 parent 2aa4e5f commit ff1592f

File tree

4 files changed

+450
-5
lines changed

4 files changed

+450
-5
lines changed

katas/roman-numerals/package-lock.json

Lines changed: 325 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

katas/roman-numerals/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"test": "mocha -w"
55
},
66
"devDependencies": {
7+
"chai": "^4.1.2",
78
"mocha": "3.2.0"
89
}
910
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
module.exports = function toRomanNumber(num) {
4+
5+
const romanNums = {
6+
1: 'I',
7+
2: 'II',
8+
3: 'III',
9+
10+
5: 'V',
11+
6: 'VI',
12+
10: 'X',
13+
50: 'L',
14+
100: 'C',
15+
500: 'D',
16+
1000: 'M'
17+
}
18+
19+
const arabicNums = Object.keys(romanNums);
20+
let i = arabicNums.length - 1;
21+
let roman = "";
22+
while (i >= 0){
23+
if (parseInt(arabicNums[i]) > num) {
24+
i = i-1;
25+
}
26+
else {
27+
roman += romanNums[arabicNums[i]];
28+
num = num % parseInt(arabicNums[i]);
29+
}
30+
}
31+
32+
33+
return roman;
34+
35+
36+
}

0 commit comments

Comments
 (0)