HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Generators in Javascript; Project Euler #2

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptprojectgeneratorseuler

Problem

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 ')
var time_post = performance.now()
document.write('completed in ' + Math.round((time_post - time_pre)*100)/100 + ' seconds')

Solution

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];
  }
}

Code Snippets

var current = a = b = 1; // a and b are globals
var current, a, b; // all variables are local
current = a = b = 1;
sequence = fib_gen(); // not declared
even_fibs_total = 0; // not declared
cur = sequence.next().value; // not declared
var sequence = fib_gen();
var even_fibs_total = 0;
var cur = sequence.next().value;
function solution() {
  var evenFibsTotal = 0;

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

  return evenFibsTotal;
}

Context

StackExchange Code Review Q#124997, answer score: 5

Revisions (0)

No revisions yet.