DEV Community

duccanhole
duccanhole

Posted on

code every day with me

--DAY ONE--
Hi, I am going to make #100DaysOfCode Challenge. I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
This is First day:
-Problem: Roman to integer
-Link: https://leetcode.com/problems/roman-to-integer/
*My solution (javascript):

var romanToInt = function(s) {
    let num = new Map([
        ['I',1], ['V',5], ['X',10], ['L',50], ['C',100], ['D',500], ['M',1000]
    ]),sum=0;
    for(let i=0;i<s.length;i++){
        if (i == s.length - 1) sum += num.get(s[i])
      else {
        let first = num.get(s[i]);
        let second = num.get(s[i + 1]);
        if (first >= second) sum += first;
        else sum -= first;
      }
    }
    return sum;
};
Enter fullscreen mode Exit fullscreen mode

--> If you have better solution or any question, please comment below. I will appreciate.

Discussion (0)