任何人都可以解釋這個while循環對我嗎?它是免費代碼營羅馬數字挑戰的一部分 - 這不是我的,我做了我自己的,但它是更多的代碼。我試圖理解這一點來改善我自己。無論如何,while循環完全拋棄了我。任何幫助都是極好的。while循環javascript邏輯
function romans(num){
var roman = "";
var romanNumerals = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
var numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
for (var i=0; i<numbers.length; i++) {
//If the num was 5 then then it would read 5 greater than 1000?
while (num >= numbers[i]) {
roman = roman + romanNumerals[i]
//5 minus 1000?
num = num - numbers[i]
}
}
return roman;
}
console.log(romans(5))
'// 5減去1000?'只有當數學被完全破壞並且5> = 1000'纔會是真的:)對於' num === 5''while'循環只會爲'i === 10'執行一次' – Andreas