Skip to content

Instantly share code, notes, and snippets.

@emmabostian
Created June 20, 2019 13:08
Show Gist options
  • Save emmabostian/955d8d21b2ae297036bbd2b14a07d0e8 to your computer and use it in GitHub Desktop.
Save emmabostian/955d8d21b2ae297036bbd2b14a07d0e8 to your computer and use it in GitHub Desktop.
function findProduct(arr) {
// First find the total product of all indeces
let totalProduct = arr[0];
for (let i = 1; i < arr.length; i++) {
totalProduct *= arr[i];
}
// Then, return a new array where each index is the
// product of all numbers, except for the current value of that index
// i.e. newArr[0] = arr[1] * arr[2] * arr[3]... arr[length-1];
// i.e. newArr[1] = arr[0] * arr[2] * arr[3]... arr[length-1];
// We can do this by just dividing by the number at arr[i] to get the
// remaining product
return arr.map(num => totalProduct / num);
}
@segebee
Copy link

segebee commented Jun 21, 2019

i love the reasoning behind this. great job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment