Skip to content

Instantly share code, notes, and snippets.

@RayLuxembourg
Created February 11, 2019 04:12
Show Gist options
  • Save RayLuxembourg/8cd203ce1fcdddaa50972fe1ed23b0cb to your computer and use it in GitHub Desktop.
Save RayLuxembourg/8cd203ce1fcdddaa50972fe1ed23b0cb to your computer and use it in GitHub Desktop.
Roman Numerics
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