DEV Community

duccanhole
duccanhole

Posted on

5

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.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay