-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。
例如,如果您通过以下方式调用该函数: adjacentElementsProduct([3,6,-2,-5,7,3]); –
'++ i'有'i + 1'没有的副作用。这应该是足够的提示。 – zwol