给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...A[i-1]A[i+1]...A[n-1]。不能使用除法。
function multiply(array) {
let B = []
let change = false
for (let i = 0; i < array.length; i++) {
B[i] = 1
for (let j = 0; j < array.length; j++) {
let temp = 1
if (i === j) {
change = true
temp = array[j]
array[j] = 1
}
B[i] *= array[j]
if (change) {
array[j] = temp
change = false
}
}
}
return B
}
console.log(multiply([1,2,3,4,5]))