Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict"
- function RomanToInt(s)
- {
- const RomanN = {
- "I":1,
- "V":5,
- "X":10,
- "L":50,
- "C":100,
- "D":500,
- "M":1000
- }
- let sum =0
- let num =0
- let pattern = "[IVXLCDM]"
- const re = new RegExp(pattern)
- if (re.exec(s,pattern))
- {
- for (let i = 0; i < s.length; i++)
- {
- var jelenlegiChar = s[i]
- num = RomanN[jelenlegiChar]
- if ((i + 1 < s.length) && (RomanN[s[i+1]] > RomanN[jelenlegiChar]))
- {
- sum -= num
- }
- else
- {
- sum += num
- }
- }
- }
- else{
- document.write("Római számot nem tartalmaz")
- }
- document.write(sum)
- }
- RomanToInt("VIII");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement