Solving Fibonacci even sum

I wanted to write about a problem we had as practice for learning JavaScript and my thought process for solving it.  The problem was from Project Euler and the instructions are as follows:

“By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.”

The first step to solving this problem is to define what a fibonacci sequence is and figure out how to generate the numbers in the sequence up to a given value.  Each number in the fibonacci sequence is generated by adding the previous two numbers together.  Depending on who you ask, the first two numbers of the sequence are either 0 and 1 or 1 and 1.  I decided to start with 0 and 1.

I started by naming my function fibonacciEvenSum that takes one parameter num. Based on the problem definition, the Fibonacci values can’t exceed 4,000,000 so num will be set to that number.  I declared the variable fibonacci to hold all the values generated for the Fibonacci sequence and set it equal to [0,1], the first two values of the sequence.  I also declared the variable sum, which will hold the sum of the even numbers, and set it equal to zero.

function fibonacciEvenSum(num) {

var fibonacci = [0,1];


var sum = 0;


Next, I created a for loop that would generate all of the Fibonacci sequence values less than or equal to the given num. The loop starts at index position 2 because the first two values in the array are already defined, so we need to start with the third value. The loop then calculates the current value in the sequence by adding the previous number (fibonacci[i-1]) and the number before that (fibonacci[i-2]). Once the loop finishes, it will have generated a Fibonacci sequence with values less than or equal to the given num.

for(let i=2; fibonacci[i-1] + fibonacci[i-2] <= num; i++) {

fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];


}


The final step is to add together all of the even numbers in the sequence. To do this, I created another for loop that runs through all of the numbers in the sequence, checks if the number is even, and if it is, adds it to the current sum.

for(let j=0; j < fibonacci.length; j++) {

if(fibonacci[j]%2 === 0) {


sum += fibonacci[j];


}


}


The final step of the function is to return the sum.

return sum;

}


I came back to this problem later because I wanted to see if there was a way to reduce the steps and make the solution simpler. I looked at both of the for loops to see if there was a way to combine them. I decided that I could use the first loop to create the sequence and then after the next number was added, I could do my check to see if it was even and then add it to sum if it is.

function fibonacciEvenSum(num) {

var fibonacci = [0,1]


var sum = 0;


for(let i=2; fibonacci[i-1] + fibonacci[i-2] <= num; i++) {


fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];


if(fibonacci[i]%2 === 0) {


sum += fibonacci[i];


}


}


return sum;


}


I like these kind of abstract problems because they really require you break down the problem and think about each step that needs to be completed and then figure out how to put it all together. It’s also good practice for thinking about how you can combine steps to create a simpler solution. I look forward to tackling more of these in the future.