scrimba
Solution for day 9 of #javascriptmas
Go Pro!

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 fibonacciOddTotal = 0;//keep track of total odd numbers
let previousFibonacciNumber = 0; //track the previous number in the series
let stepSum =0;//filler spot to hold the new current value while I shift older values
let currentFibonacciNumber = 1; //current value
do{
//check if current number is odd if dividing by 2 has no remainder
if(currentFibonacciNumber % 2 !== 0){
fibonacciOddTotal += currentFibonacciNumber;
}

//get the next step by summing current and previous
stepSum = previousFibonacciNumber + currentFibonacciNumber;

//set move current value to pervious
previousFibonacciNumber = currentFibonacciNumber;
//set the current as the new stepsum
currentFibonacciNumber = stepSum;

}while(num >= stepSum);
return fibonacciOddTotal;
}



/**
* 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