Last active
May 5, 2021 10:02
-
-
Save alialaba/3da8855eb92e47c6b36e23baea7645c0 to your computer and use it in GitHub Desktop.
This file contains 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 productNumber = (num) => { | |
let products = [num.length]; | |
//forward pass | |
let forwardMultiple = 1; | |
for (let i = 0; i < num.length; i++) { | |
products[i] = forwardMultiple; | |
forwardMultiple *= num[i]; | |
} | |
//backword pass | |
let backwordMultiple = 1; | |
for (let i = num.length - 1; i >= 0; i--) { | |
products[i] *= backwordMultiple; | |
backwordMultiple *= num[i]; | |
} | |
return products; | |
} | |
console.log(productNumber([4, 5, 10, 2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @alialaba, congratulations, you are one of the winners of the $20 award 🎉🎉for Week 4 of #AlgorithmFridays.
Your solution was selected because it is optimal, well-explained through use of good commenting and also handles the edge case for when one of the numbers in the array is 0. Awesome stuff!
We will contact you between 24 - 48 hours for your award.
Congratulations once again and thanks for participating!