[英]breaking a for loop with length decrement
我發現這段代碼將 excel 列名轉換為列號,但很難理解循環的中斷條件
//function convert excel sheet column to number
toColumnNumber = (colName) => {
let result = 0;
for (let i = colName.length, j = 0; i--; j++) {
result += Math.pow(26, i) * (colName.charCodeAt(j) - 64);
}
return result;
};
console.log(toColumnNumber("AB"));
它使用i--
作為中斷條件,我不明白如何使用它來中斷循環。 或者這就是當我們使用 i-- 作為中斷條件並且它達到 0 時 javascript 的工作方式它打破了循環?
當我們不斷遞減i
的值時,最終它會達到0
,這將是falsy
的並會停止循環。
這只是一種奇特的做法:
for (let i = colName.length, j = 0; i > 0; j++, i--) {
result += Math.pow(26, i) * (colName.charCodeAt(j) - 64);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.