Created
July 13, 2017 16:55
-
-
Save Sara3/3e7630b0bc6ac3d3c94a6d1aebef19c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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