Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nirajkrz/df9a3cbb371b926ebeb4 to your computer and use it in GitHub Desktop.
Save nirajkrz/df9a3cbb371b926ebeb4 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Sum All Odd Fibonacci Numbers
// Bonfire: Sum All Odd Fibonacci Numbers
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-odd-fibonacci-numbers
// Learn to Code at Free Code Camp (www.freecodecamp.com)
//Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number.
function sumFibs(num) {
var prevNumber = 0;
var currNumber = 1;
var result = 0;
while (currNumber <= num) {
if (currNumber % 2 !== 0) {
result += currNumber;
}
var added = currNumber + prevNumber;
prevNumber = currNumber;
currNumber = added;
}
return result;
}
sumFibs(4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment