문제 링크 : Product of Array Except Self
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
const multiple = nums.reduce((acc,cur,idx,origin) => {
if( acc !== 0 && cur !== 0) {
return acc * cur
} else {
return acc === 0 ? cur : acc
}
})
const isHaveZeros = nums.filter(e => e === 0).length > 1
const isHasZero = nums.includes(0)
let result = []
nums.forEach( e => {
if(isHaveZeros) {
result.push(0)
} else {
if(isHasZero && e !== 0){
result.push(0)
}else if(isHasZero && e === 0) {
result.push(multiple)
} else if(!isHasZero) {
result.push(multiple/e)
}
}
})
return result
};