Created
December 3, 2015 02:44
-
-
Save anonymous/bd5837128b53948793c6 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Sum All Odd Fibonacci Numbers
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
// 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