-4
它工作正常(至少對我來說),直到我嘗試將英文字母翻譯成羅馬數字。英文到拉丁文的文本翻譯
我想將「18」翻譯成「XVII」和「1212」爲「MCCXII」。另外,有沒有辦法將「1,212」翻譯成「MCCXII」?
這裏的所有代碼:
<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>
而且
該代碼的哪一部分甚至*嘗試*轉換數字?當然,您可以通過簡單的Google搜索找到足夠多的羅馬數字轉換器示例嗎? – JJJ
請不要使用零時間設置'setInterval'!改爲使用['addEventListener'](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)。 – Mottie