Advertisement
angryatti

Roman Number To Decimal v2

Apr 16th, 2023
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="hu">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Római szémok</title>
  8. </head>
  9. <body>
  10.    
  11. <form action="post">
  12. <label for="inputR">Római szám:</label>
  13. <input type="text" name="inputRomai" id="inputR">
  14.  
  15. <button type="submit" onclick="RomanToInt(document.getElementById('inputR').value)">Átvált</button>
  16.  
  17. </form>
  18.  
  19.  
  20. <script>
  21. "use strict"
  22.  
  23. function RomanToInt(s)
  24. {
  25.  
  26.  
  27. const RomanN = {
  28.     "I":1,
  29.     "V":5,
  30.     "X":10,
  31.     "L":50,
  32.     "C":100,
  33.     "D":500,
  34.     "M":1000
  35. }    
  36. let sum =0
  37. let num =0
  38. let pattern = "[IVXLCDM]"
  39.  
  40. const re = new RegExp(pattern)
  41.  
  42. if (re.exec(s,pattern))
  43. {
  44.  
  45.  
  46.  
  47.  
  48.  
  49.     for (let i = 0; i < s.length; i++)
  50.     {
  51.         let currentChar = s[i]
  52.  
  53.        
  54.      
  55.         num = RomanN[currentChar]
  56.    
  57.  
  58.         if ((i + 1 < s.length) && (RomanN[s[i+1]] > RomanN[currentChar]))
  59.         {
  60.             sum -= num
  61.         }
  62.         else
  63.         {
  64.             sum += num
  65.         }
  66.     }
  67. }
  68. else{
  69. document.write("Római számot nem tartalmaz")
  70.  
  71. }
  72.     document.write(sum)
  73.  
  74. }
  75. //RomanToInt("VIII");
  76.  
  77.  
  78.  
  79.  
  80. </script>
  81.  
  82.  
  83. </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement