scrimba
Note at 0:00
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

Note at 0:00
AboutCommentsNotes
Note at 0:00
Expand for more info
main.js
run
preview
console
function sumOddFibonacciNumbers(num) {
let fibPrev=1;
let fib=0;
let sum=0;

while (fibPrev < num) {
[ fibPrev, fib ] = [fib + fibPrev, fibPrev];

if (fib % 2 != 0) {
sum += fib;
}
}

return sum;
}



/**
* Test Suite
*/
describe('sumOddFibonacciNumbers()', () => {
it('returns sum of all odd Fibonnci numbers', () => {
const num = 10;
const result = sumOddFibonacciNumbers(num);
console.log("result 1: ", result);
expect(result).toBe(10);
});

it('returns sum of all odd Fibonnci numbers 2nd example', () => {
const num = 1000;
const result = sumOddFibonacciNumbers(num);
console.log("result 2: ", result);
expect(result).toBe(1785);
});

it('returns sum of all odd Fibonnci numbers 3rd example', () => {
const num = 4000000;
const result = sumOddFibonacciNumbers(num);
console.log("result 2: ", result);
expect(result).toBe(4613732);
});
});
Console
"result 2: "
,
1785
,
"result 1: "
,
10
,
"result 2: "
,
4613732
,
/index.html
LIVE