scrimba
Solution for day 9 of #javascriptmas
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Solution for day 9 of #javascriptmas
AboutCommentsNotes
Solution for day 9 of #javascriptmas
Expand for more info
main.js
run
preview
console
function sumOddFibonacciNumbers(num) {
let fibNumbers = [1,1];
let sum = 2;
let nextFib = 2;
// generate fibonacci numbers until num
// at the same time add to the sum if the number is odd
while (nextFib < num){
if (nextFib % 2 !== 0) {
sum += nextFib;
}
// calculate next fib
nextFib = fibNumbers[fibNumbers.length - 1] + fibNumbers[fibNumbers.length - 2];
fibNumbers.push(nextFib);
}
return sum;
}


/**
* Test Suite
*/
describe('sumOddFibonacciNumbers()', () => {
it('returns sum of all odd Fibonnci numbers', () => {
// arrange
const num = 10;

// act
const result = sumOddFibonacciNumbers(num);

// log
console.log("result 1: ", result);

// assert
expect(result).toBe(10);
});

it('returns sum of all odd Fibonnci numbers 2nd example', () => {
// arrange
const num = 1000;

// act
const result = sumOddFibonacciNumbers(num);

// log
console.log("result 2: ", result);

// assert
expect(result).toBe(1785);
});
});
Console
"result 2: "
,
1785
,
"result 1: "
,
10
,
/index.html
LIVE