2017-04-22 58 views
-1

這怎麼能給我不同的結果?++ i和i + 1在javascript中的區別是什麼

唯一的區別是在[++ i]和第[i + 1]

function adjacentElementsProduct(inputArray) { 
    total = inputArray[0] * inputArray[1]; 

    for (i = 1; i < inputArray.length-1; i++) { 
    mul = inputArray[i] * inputArray[++i]; 
    if (total < mul) 
     total = mul; 
     } 
    return total; 
} 

    function adjacentElementsProduct(inputArray) { 
    total = inputArray[0] * inputArray[1]; 

    for (i = 1; i < inputArray.length-1; i++) { 
    mul = inputArray[i] * inputArray[i+1]; 
    if (total < mul) 
     total = mul; 
     } 
    return total; 
} 

感謝您的幫助。

這個問題被標記爲重複,但其他問題是關於i ++和我的關於++ i。

+0

例如,如果您通過以下方式調用該函數: adjacentElementsProduct([3,6,-2,-5,7,3]); –

+0

'++ i'有'i + 1'沒有的副作用。這應該是足夠的提示。 – zwol

回答

相關問題