Created
February 11, 2019 04:12
-
-
Save RayLuxembourg/8cd203ce1fcdddaa50972fe1ed23b0cb to your computer and use it in GitHub Desktop.
Roman Numerics
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
const romanNumerics = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000 | |
}; | |
const printRomanNumericValue = chars => { | |
let positives = 0; | |
let negatives = 0; | |
for (let i = 0; i < chars.length; i++) { | |
const numValue = romanNumerics[chars.charAt(i)]; | |
const nextNumValue = romanNumerics[chars.charAt(i + 1)] || 0; | |
numValue >= nextNumValue | |
? (positives += numValue) | |
: (negatives += numValue); | |
} | |
return positives - negatives; | |
}; | |
console.log(printRomanNumericValue("XXVI")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment