description :
i expect the indexPlus 2 but it comes 11,what is wrong? and why?
see it in line 28 .
code:
var inputArr = [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0]
console.log('before', inputArr)
var arr = []
//先遍历inputArr确定0的位置
for (key in inputArr) {
if (inputArr[key] === 0) {
arr.push(key)
}
}
//将0移到数组最后面,通过位置交换实现
for (value of arr) {
for (i = value; i < inputArr.length - 1; i++) {
swap(i, i + 1)
}
}
function swap(index, indexPlus) {
var temp = inputArr[index]
inputArr[index] = inputArr[indexPlus]
inputArr[indexPlus] = temp
}
console.log('after', inputArr)