-4

It works fine (for me at least) until I try to translate English letters into Roman Numerals.

I want to to translate "18" into "XVII" and "1212" into "MCCXII". Also, is there a way it can still translate "1,212" into "MCCXII"?

Here's all the code:

<html>
<head>
<link rel="icon" href="http://claof.com/ClaofIcon.ico?v=2"></link/>
<title>English to Latin</title>
<body bgcolor="lightgray">
<center><input style="text-align:center;width:100%" placeholder="English text..." id="english"></input><br>
<a id="latin"></a></center>
</body>
<script>
xx=0
function replace_text() {
document.getElementById('english').value = document.getElementById('english').value.toUpperCase()
str = document.getElementById('english').value.toLowerCase()



if (document.getElementById('english').value.length) {
for (i=100000;i>0;i--) {
number = document.getElementById('english').value.match(/\d+/)[0]
v=i
v.toString()
if (number.match(v)) {
xx = toRoman(number.match(v))
}
}
}


str = str.split("").reverse().join("")
str = str.replace(/a/g, "");
str = str.replace(/b/g, "");
str = str.replace(/c/g, "");
str = str.replace(/d/g, "");
str = str.replace(/e/g, "");
str = str.replace(/f/g, "");
str = str.replace(/g/g, "G");
str = str.replace(/z/g, "");
str = str.replace(/h/g, "");
str = str.replace(/i/g, "");
str = str.replace(/j/g, "");
str = str.replace(/k/g, "");
str = str.replace(/l/g, "");
str = str.replace(/m/g, "");
str = str.replace(/n/g, "");
str = str.replace(/o/g, "");
str = str.replace(/p/g, "");
str = str.replace(/q/g, "");
str = str.replace(/r/g, "");
str = str.replace(/s/g, "");
str = str.replace(/t/g, "");
str = str.replace(/v/g, "");
str = str.replace(/w/g, "");
str = str.replace(/x/g, "");
str = str.replace(/y/g, "Y");
str = str.replace(/u/g, "");
document.getElementById('latin').innerHTML = str
}
setInterval("replace_text()",0)

function toRoman(num) {  
  var result = '';
  var decimal = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  var roman = ["M", "CM","D","CD","C", "XC", "L", "XL", "X","IX","V","IV","I"];
  for (var i = 0;i<=decimal.length;i++) {
  // looping over every element of our arrays
    while (num%decimal[i] < num) {   
    // keep trying the same number until it won't fit anymore      
      result += roman[i];
      // add the matching roman number to our result string
      num -= decimal[i];
      // remove the decimal value of the roman number from our number
    }
  }
  return result;
}
</script>
</head>
</html>

And

Claof
  • 3
  • 3
  • Which part of that code even *tries* to convert numerals? Surely you can find more than enough examples of Roman numeral converters with a simple Google search? – JJJ Jan 01 '17 at 21:54
  • 1
    Please don't ever set `setInterval` with a zero time! Use [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) instead. – Mottie Jan 01 '17 at 21:59

1 Answers1

0

Here is the roman number convertor Convert a number into a Roman Numeral in javaScript

Remove the "setInterval("replace_text()",0)" and rather use this "document.getElementById('english').addEventListener("keyup", replace_text);"

Community
  • 1
  • 1
devondre
  • 493
  • 6
  • 14