-
Notifications
You must be signed in to change notification settings - Fork 2
Algorithm Sum All Odd Fibonacci Numbers
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
You will need to gather all the Fibonacci numbers and then check for the odd ones. Once you get the odd ones then you will add them all. The last number should be the number given as a parameter if it actually happens to be an off Fibonacci number.
To get the next number of the series, you need to add the current one to the previous and that will give you the next one.
try to solve the problem now
To check if a number is even all you have to check is if number % 2 == 0
.
try to solve the problem now
As you get the next odd one, don't forget to add it to a global variable that can be returned at the end. result += currNumber;
will do the trick.
try to solve the problem now
Solution ahead!
function sumFibs(num) {
var prevNumber = 0;
var currNumber = 1;
var result = 0;
while (currNumber <= num) {
if (currNumber % 2 !== 0) {
result += currNumber;
}
currNumber += prevNumber;
prevNumber = currNumber - prevNumber;
}
return result;
}
// test here
sumFibs(4);
🚀 Run Code
- Create a variable to keep record of the current and previous numbers along with the result that will be returned.
- Use a while loop to make sure we do not go over the number given as parameter.
- We use the modulo operand to check if the current number is odd or even. If it is even, add it to the result.
- Complete the Fibonacci circle by rotating getting the next number and swapping values after.
- Return the result.
function sumFibs(num) {
// create an array of fib numbers till num
var arrFib = [1];
for (var i = 1; i <=num;) {
arrFib.push(i);
i = arrFib[arrFib.length - 1] + arrFib[arrFib.length - 2];
}
// return the sum of odd numbers from the array
var res = arrFib.reduce(function(prev, curr) {
if (curr%2 !== 0) return prev + curr;
else return prev;
});
return res;
}
// test here
sumFibs(4);
🚀 Run Code
- Create an array of fibonacci numbers till num.
- Use
reduce()
method to find the sum of odd members of array. - Return the sum.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @Rafase282 @d3ddd for your help with Algorithm: Sum All Odd Fibonacci Numbers
⚠️ 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.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links