Last active
June 4, 2018 06:56
-
-
Save istiyakamin/cf7d8df5df49883d97717683670bc32e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function roman_to_Int(str1) { | |
if(str1 == null) return -1; | |
var num = char_to_int(str1.charAt(0)); | |
var pre, curr; | |
for(var i = 1; i < str1.length; i++){ | |
curr = char_to_int(str1.charAt(i)); | |
pre = char_to_int(str1.charAt(i-1)); | |
if(curr <= pre){ | |
num += curr; | |
} else { | |
num = num - pre*2 + curr; | |
} | |
} | |
return num; | |
} | |
function char_to_int(c){ | |
switch (c){ | |
case 'I': return 1; | |
case 'V': return 5; | |
case 'X': return 10; | |
case 'L': return 50; | |
case 'C': return 100; | |
case 'D': return 500; | |
case 'M': return 1000; | |
default: return -1; | |
} | |
} | |
console.log(roman_to_Int('XXVI')); | |
console.log(roman_to_Int('CI')); | |
console.log(roman_to_Int('XI')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment