Skip to content

Commit e102272

Browse files
authored
Update javascript.json
Added roman to integer code snippet
1 parent 69e9d13 commit e102272

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

public/data/javascript.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@
1313
}
1414
]
1515
},
16+
{
17+
"title": "Roman to integer",
18+
"description": "Converts roman numeric system into integers",
19+
"code": [
20+
"const romanToInt = function(roman) {",
21+
" let symbols = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 };",
22+
" roman = Array.from(roman);",
23+
" let output = 0, toMiss = [];",
24+
" for(let i = 0; i < roman.length; i++) {",
25+
" let curr = symbols[roman[i]];",
26+
" let next = symbols[roman[i+1]];",
27+
"",
28+
" if(!toMiss.includes(i)) {",
29+
" if(next > curr) {",
30+
" output+=(next-curr);",
31+
" toMiss.push(i+1);",
32+
" } else {",
33+
" output+=curr;",
34+
" }",
35+
" }",
36+
" }",
37+
" return output;",
38+
"};",
39+
"",
40+
"// Usage:",
41+
"console.log(romanToInt(\"IXV\")); // Output: '14'"
42+
],
43+
"tags": ["javascript", "number", "format", "roman", "utility"],
44+
"author": "r3medy"
45+
},
1646
{
1747
"categoryName": "Array Manipulation",
1848
"snippets": [

0 commit comments

Comments
 (0)