JavaScript Challenge - Product of Everything Else

  1. Find the product of all numbers except 0. Find all zeros and index of first zero
  2. If two or more of number = 0 than all result items = 0
  3. If one number = 0 than only result item with this index = product, all other = 0
  4. If no 0 numbers than all result items = product/number
let zeros = 0;
  let zeroIndex = undefined;
  let product = 1;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === 0) {
      if (zeros === 0) zeroIndex = i;
      zeros++;
    } else {
      product *= arr[i];
    }
  }
  let res = [];
  if (zeros > 0) {
    res = new Array(arr.length).fill(0);
    if (zeros < 2) {
      res[zeroIndex] = product;
    }
    return res;
  }
  for (let i = 0; i < arr.length; i++) {
    res.push(Math.round(product / arr[i]));
  }
  return res;
function productOfTheOthers(array) {
  return array.map((el, i, arr) => [...arr].reduce((prod, item, idx) => {
       if(i===idx) {
         return prod
       }
       return prod * item
     }, 1));
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]));
function productOfTheOthers(array) { // Write your code here const productOfAll = (arr, skipIndex) => arr.reduce((prev, current, i) => { if (i !== skipIndex) prev *= current; return prev; } , 1); return array.map((_, i, original) => productOfAll(original, i)); } console.log(productOfTheOthers([1, 2, 3, 4, 5])) // Leave this so we can test your code: module.exports = productOfTheOthers;