Skip to content

Instantly share code, notes, and snippets.

@osahondev
Last active May 2, 2021 22:52
Show Gist options
  • Select an option

  • Save osahondev/109185d053f986cea0587d7092006e6a to your computer and use it in GitHub Desktop.

Select an option

Save osahondev/109185d053f986cea0587d7092006e6a to your computer and use it in GitHub Desktop.
const productsArray = nums =>{
if( nums == undefined || nums == null ) return [];
if( !Array.isArray(nums) ) return [];
if( nums.length == 0 ) return [];
if( nums.length == 1 ) return[0];
let product = 1;
for(let [index,number] of nums.entries()){
product = number * product;
}
for( let [j,number] of nums.entries() ){
nums[j] = product/number;
}
return nums;
}
@osahondev
Copy link
Author

Hello @meekg33k, thank you for your feedback as always.

Please find below my comments.

I have made two changes to the function.

  1. To handle the edge case of an array length of 1
  2. Rather than return a new array with a map function, I have modified each index of the "nums" array and return the input back.

I would be happy to know your thought about this approach.

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