var tr = { "I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000 } function solution (roman_string) { return roman_string.split('').map((letter, index, a) => { const current_value = tr[letter]; const next_value = tr[a[index+1]]; return next_value && current_value < next_value ? (- current_value) : current_value; }).reduce((acc, item) => acc + item, 0); }
- var tr = {
- "I": 1,
- "V": 5,
- "X": 10,
- "L": 50,
- "C": 100,
- "D": 500,
- "M": 1000
- }
function solution (roman) {letters = roman.split('');var res = 0;letters.forEach((cur, i) => {var next = letters[i + 1];if (tr[next] && tr[cur] < tr[next]) {res -= tr[cur];} else {res += tr[cur];}})return res;- function solution (roman_string) {
- return roman_string.split('').map((letter, index, a) => {
- const current_value = tr[letter];
- const next_value = tr[a[index+1]];
- return next_value && current_value < next_value ?
- (- current_value) : current_value;
- }).reduce((acc, item) => acc + item, 0);
- }
const strictEqual = require('chai').assert.strictEqual; function doTest (romanString, expected) { const actual = solution(romanString); strictEqual(actual, expected, `for roman number ${romanString}`); } describe("Tests", () => { it("sample tests", () => { doTest('XXI', 21); doTest('I', 1); doTest('IV', 4); doTest('MMVIII', 2008); doTest('MDCLXVI', 1666); doTest('MMMDCCXXIV', 3724); }); });
- const strictEqual = require('chai').assert.strictEqual;
- function doTest (romanString, expected) {
- const actual = solution(romanString);
- strictEqual(actual, expected, `for roman number ${romanString}`);
- }
- describe("Tests", () => {
- it("sample tests", () => {
- doTest('XXI', 21);
- doTest('I', 1);
- doTest('IV', 4);
- doTest('MMVIII', 2008);
- doTest('MDCLXVI', 1666);
- doTest('MMMDCCXXIV', 3724);
- });
- });