Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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')
share|improve this question

1) You have a few undeclared variables which means they become global. You want to use var (or let or const) always. Compare this:

var current = a = b = 1; // a and b are globals

To:

var current, a, b; // all variables are local
current = a = b = 1;

Same here:

sequence = fib_gen(); // not declared
even_fibs_total = 0; // not declared
cur = sequence.next().value; // not declared

Compare to:

var sequence = fib_gen();
var even_fibs_total = 0;
var cur = sequence.next().value;

2) Conventions:

  • Name your variables with camelCase instead snake_case.
  • Use === instead of == unless you know you really really need == (tip: you probably don't).
  • Always write if statements with braces, even if one line, for better readability and refactoring.

3) Generators are iterators, and iterators can be iterated with the for..of loop. If your environment supports generators it probably supports for..of:

function solution() {
  var evenFibsTotal = 0;

  for (var cur of fibGen()) {
    if (cur >= 4000000) {
      break;
    }
    if (cur % 2 === 0) {
      evenFibsTotal += cur;
    }
  }

  return evenFibsTotal;
}

4) If you have ES6 support (or compilation via Babel) you can use destructuring assignment to simplify the fibonnaci function:

function* fibGen() {
  var a = 1;
  var b = 1;
  while (true) {
    yield b;
    [a, b] = [b, a + b];
  }
}
share|improve this answer
    
thanks for the helpful advice! can you go a little more into the global vs. local scoping when using the var keyword? in addition, when would the for... of vs. for... in iteratator syntax be used? in python it's all the same so i'm not familiar with it. – mburke05 yesterday
    
Is there any reason to use var instead of let? – Peter Olson yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.