Last active
May 2, 2021 22:52
-
-
Save osahondev/109185d053f986cea0587d7092006e6a 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
| 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; | |
| } |
Author
Hello @meekg33k, thank you for your feedback as always.
Please find below my comments.
I have made two changes to the function.
- To handle the edge case of an array length of 1
- 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
Hello @enaiho, thank you for participating in Week 4 of Algorithm Fridays and more importantly, thank you for your comments on other participants' solution.
Your solution is clean, robust and efficient. especially with time complexity. However, do you think we can better optimize this in terms of memory usage?
Kudos all the same for a very well implemented solution!