I have a background in Linguistics and my journey as an aspiring developer has just begun.
I was hoping you could give me a feedback on the solution I have come up with for the second challenge of Project Euler, and maybe explain me why many of the other solutions I have come across were wrapped inside a single function.
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.
let fibSequence = [0, 1];
let result = 0;
function fibonacci(array) {
return array[array.length - 1] + array[array.length - 2];
}
while(fibSequence[fibSequence.length - 1] < 4e+6) {
fibSequence.push(fibonacci(fibSequence));
}
fibSequence.forEach(function (number) {
if(number % 2 === 0) {
result += number;
}
});
console.log(result);