Skip to content

Commit

Permalink
Raza khan9639 (#9)
Browse files Browse the repository at this point in the history
* Hactoberfest Contribution Problem#13

* Hactoberfest Contribution Problem#13
  • Loading branch information
RazaKhan9639 authored Oct 9, 2022
1 parent 11d7ed8 commit 3a573db
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions JS/RomanToInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//Name: Raza Mohayyuddin

//Username: RazaKhan9639

//Approach: First create a hashmap to store values of each key, this can be optimized to use string array or any other method. After that create a variable named sum to store the sum of the. After that start iterating over the array & check if the character is less than the next character then add the values to sum till checked all characters. After that return the value of the sum variable.

var ro = new Map();
ro.set("I", 1);
ro.set("V", 5);
ro.set("X", 10);
ro.set("L", 50);
ro.set("C", 100);
ro.set("D", 500);
ro.set("M", 1000);
var romanToInt = function (s) {
var sum = 0;
var n = s.length;

for (i = 0; i < n; i++) {
if (i != n - 1 && ro.get(s.charAt(i)) < ro.get(s.charAt(i + 1))) {
sum += ro.get(s.charAt(i + 1)) - ro.get(s.charAt(i));
i++;
} else {
sum += ro.get(s.charAt(i));
}
}
return sum;
};

0 comments on commit 3a573db

Please sign in to comment.