// Each new term in the Fibonacci sequence is generated by adding the previous two terms. // By starting with 1 and 2, the first 10 terms will be: // // 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... // // By considering the terms in the Fibonacci sequence whose values do not exceed four million, // find the sum of the even-valued terms. function fibonacciEvenSum(number) { "use strict"; var array = [1, 2], // initial values of fibonacci nextFibonacci = 0, // container for next fibonacci value result = 0; // container for sum of even fibonacci values while (true) { nextFibonacci = array[array.length - 2] + array[array.length - 1]; // sums the last element and second to last element of array if (nextFibonacci < number) { // to evaluate whether the next Fibonacci value is still less than number (or 4,000,000) array.push(nextFibonacci); // pushes the next Fibonacci value onto the array } else { break; // if the next Fibonacci is greater than 'number' we exit the loop } } array.forEach(function (value) { // loop through the array if (value % 2 === 0) { // evaluate only if the value is an even number result += value; // add the even fibonacci values to result variable } }); return result; } console.log(fibonacciEvenSum(4000000)); // Output: 4613732