Skip to content

Commit

Permalink
- Ali hard
Browse files Browse the repository at this point in the history
  • Loading branch information
PG-Momik committed Aug 11, 2024
1 parent 20ce396 commit 3bdad29
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions _003_romanToInteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function (s) {
const convertMap = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}

let sum = 0;
let prev = undefined;

for(let iterations = s.length -1; iterations >=0; iterations --){
let num = convertMap[s[iterations]];

if(iterations === s.length -1){
prev = num;
sum = sum+num;
continue;
}

sum = num < prev ? sum-num: sum+num;
prev = num;
}

return sum
};

0 comments on commit 3bdad29

Please sign in to comment.