Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edc46b8

Browse files
romant to Integer complete
1 parent 37060d0 commit edc46b8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎problem71/problem71.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@ Implement a function that converts a Roman numeral to an integer. The function s
44
55
*/
66

7+
function romanToIntNum(romanNum) {
8+
let numInt = 0;
9+
const romValues = {M:1000, D:500, C:100, L:50, X:10, V:5, I:1};
10+
11+
for(let i = 0; i < romanNum.length; i++){
12+
13+
if(romValues[romanNum[i]] < romValues[romanNum[i+1]]){
14+
numInt -= romValues[romanNum[i]];
15+
}
16+
else{
17+
numInt += romValues[romanNum[i]]
18+
}
19+
}
20+
return numInt;
21+
}
22+
23+
console.log(romanToIntNum("CXM"));
24+
console.log(romanToIntNum("XX"));
25+
console.log(romanToIntNum("VI"));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /