我發現這段代碼將 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 的作業方式它打破了回圈?
uj5u.com熱心網友回復:
隨著我們不斷遞減 的值i,最終它會達到0并且falsy會停止回圈。
這只是一種奇特的說法:
for (let i = colName.length, j = 0; i > 0; j , i--) {
result = Math.pow(26, i) * (colName.charCodeAt(j) - 64);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534622.html
上一篇:如何填補每個陣列元素的空白?
