I'm trying to learn javascript using project euler and I decided to try to force myself to learn about generators in javascript using problem number 2 which asks us to: Find the sum of the even-valued terms in the fibonacci (f(n)) sequence such that f(n) < 4M , beginning with n=1 is 1, n=2 is 2, n=3 is 3 and so on
My code below presently works but I'm hoping to get some input as I began learning javascript this week. I'm coming from a python background and any input that keeps that in mind would be heavily appreciated!
function* fib_gen() {
var current = a = b = 1;
while (true) {
current = b;
yield current;
b = a + b;
a = current;
}
}
function solution() {
sequence = fib_gen();
even_fibs_total = 0;
cur = sequence.next().value;
while (cur < 4000000) {
if (cur % 2 == 0) even_fibs_total += cur;
cur = sequence.next().value;
}
return even_fibs_total
}
var time_pre = performance.now()
document.write(solution())
document.write('<br>')
var time_post = performance.now()
document.write('completed in ' + Math.round((time_post - time_pre)*100)/100 + ' seconds')