Skip to content

Instantly share code, notes, and snippets.

@Sara3
Created July 13, 2017 16:55
Show Gist options
  • Save Sara3/3e7630b0bc6ac3d3c94a6d1aebef19c0 to your computer and use it in GitHub Desktop.
Save Sara3/3e7630b0bc6ac3d3c94a6d1aebef19c0 to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
//i: array
let output = []
//need to handle division by 0 [1, 0] => [0, 1]
// need to handle division by -number [9,0,-2] => [0,-18,0]
//
let multiplyAll = nums.reduce(function(a,b){return a*b;});
if(nums.length === 2) {
output.push(nums[1])
output.push(nums[0])
//
} else {
for (let i =0; i< nums.length; i++) {
let res = multiplyAll/nums[i]
if(isNaN(res)) {
output.push(0)
}
else {
output.push(res)
}
}
}
return output;
//out: array
//multipliy all the array elements Allmultiple/num[i]
// for each element in the output array is the
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment