diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/index.md index b53a361a56..e8962cacbf 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers/index.md @@ -80,7 +80,7 @@ As you get the next odd one, don't forget to add it to a global variable that ca // Create an array of fib numbers till num const arrFib = [1, 1]; let nextFib = 0; - + // We put the new Fibonacci numbers to the front so we // don't need to calculate the length of the array on each // iteration @@ -88,11 +88,8 @@ As you get the next odd one, don't forget to add it to a global variable that ca arrFib.unshift(nextFib); } - // Sum only the odd numbers and return the value - // First, reverse the array to avoid starting acc with the first/greater number when it's even - return arrFib.reverse().reduce((acc, curr) => { - return acc + curr * (curr % 2); - }, 0); + // We filter the array to get the odd numbers and reduce them to get their sum. + return arrFib.filter(x => x % 2 != 0).reduce((a, b) => a + b); } // test here @@ -103,15 +100,19 @@ As you get the next odd one, don't forget to add it to a global variable that ca ### Code Explanation: * Create an array of fibonacci numbers till **num**. -* Use `reduce()` method to find the sum of odd members of array. +* Use `filter()` method to filter out even numbers. +* Use `reduce()` method to sum the remaining (odd) values. * Return the sum. #### Relevant Links * JS Array Prototype Push * JS For Loops Explained +* JS Array Prototype Filter * JS Array Prototype Reduce + + ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: * ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.