-1
我一直在嘗試將羅馬數字轉換爲整數,但無法找出我的代碼中的錯誤。它顯示此錯誤RangeError:無效的數組長度。有人能幫助我嗎?提前致謝。在javascript中將羅馬數字轉換爲整數
function convertToRoman(num) {
var arr = {'1':'I','5':"V",'10':"X",'50':"L",'100':"C",'500':"D",'1000':"M"} ;
var result = "";
var mult = 1;
while (num % 10 !== 0) {
var n = num % 10 ;
var h = n*mult;
if(n < 4) {
//console.log("eroor1-");
result = Array(n+1).join(arr[h.toString()]) + result;
}
else if (n == 4) {
//console.log("eroor2-");
result = arr[(h-mult).toString()] + arr[h.toString()] + result;
}
else if(n == 5) {
//console.log("eroor3-");
result = arr[h.toString()] + result;
}
else if(n < 9) {
//console.log("eroor4-");
result = Array(n-4).join(arr[mult.toString()]) + result;
//console.log("eroor4.1-");
result = arr[h.toString()] + result;
}
else if(n == 9) {
// console.log("eroor5-");
result = arr[(h+mult).toString()] + arr[h.toString()] + result;
}
else {
result = arr[h.toString()] + result;
}
mult *= 10;
num = num/10;
}
return result;
}
convertToRoman(36);
謝謝先生! –
不是問題:)如果回答了問題,請標記爲已接受 – Corbfon