- Notifications
You must be signed in to change notification settings - Fork204
Open
Description
I found that this solution doesn't handle the "Invalid Roman Numeric Value".
Ex: If we passIIXX, The output is20 which is wrong since theIIXX is invalid roman number. In this case, we throw an error, rest of the code continues..
Here is myjs solution:
functionromanToInt(s){constd={I:1,V:5,X:10,L:50,C:100,D:500,M:1000};letsumm=0;constn=s.length;leti=0;while(i<n){if(i>1&&d[s[i-2]]<d[s[i-1]]&&d[s[i]]<=d[s[i-1]]){thrownewError(`Invalid Roman numeric sequence${s}`);}if(i<n-1&&d[s[i]]<d[s[i+1]]){summ+=d[s[i+1]]-d[s[i]];i+=2;}else{summ+=d[s[i]];i++;}}returnsumm;}console.log(romanToInt('IIXX'));console.log(romanToInt('MMD'));
Note
It would be helpful if you add the example test cases when you have the solution for the problem.
Metadata
Metadata
Assignees
Labels
No labels