4

I've put together a generator to calculate the Fibonacci numbers/sequence. However, it's not working as I expect. I've "transpiled" it from python but I don't know what it is in my JavaScript code that doesn't fit my logic as python does... Can anyone give me a hint on this?

Here's the code:

// Fibonacci generator
function* fibonacci() {
  var a = 0;
  var b = 1;
  while (true) {
    yield a;
    a = b;
    b = a + b;
  }
}

// Instantiates the fibonacci generator
fib = fibonacci();

// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
  console.log(i + ' => ' + fib.next().value);
}

I think this is a matter of variable scope. I've just got this to work by doing so:

// Fibonacci generator
function* fibonacci() {
  var a = 0;
  var b = 1;
  while (true) {
    var current = a;
    a = b;
    b = current + a;
    yield current;
  }
}

// Instantiates the fibonacci generator
var fib = fibonacci();

// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
  console.log(i + ' => ' + fib.next().value);
}

However, I still don't understand why I need to declare a third variable ("current"), within my loop, to obtain the desirable result. Any ideas?

EDIT: You guys are right. The problem is the variables were being assigned in two steps and therefore not getting the desired values. Here's the final, working code, that I decided to post for future reference:

// Fibonacci generator
function* fibonacci() {
  [a, b] = [0, 1]
  while (true) {
    yield a;
    [a, b] = [b, a + b]
  }
}

// Instantiates the fibonacci generator
var fib = fibonacci();

// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
  console.log(fib.next().value);
}

Thank you very much!

2
  • Can you explain what you think is wrong? Better still, can you add a test that fails and explain what's wrong? Also, stackovflow allows you to embed your code in a runnable widget so that everyone can simply run the test you're going to add and see what's actually wrong! Commented Dec 26, 2016 at 0:58
  • 4
    Note that at b = a + b you have already set a = b, so this is just b = 2 * b. You may want to do [ a, b ] = [ b, a + b ] instead, to do the two assignments simulatenously (like in Python). Commented Dec 26, 2016 at 0:59

1 Answer 1

11

If your original code was in Python, you probably had a statement like this:

a, b = b, a + b

This will set a to b and b to a + b at the same time, which is the expected behavior for the Fibonacci sequence.

However, when you translate this code to JavaScript, you're splitting it up into two steps:

a = b
b = a + b

In this case, the first assignment is done first, and then the second one. In other words, the second assignment is affected by the first. To see how this goes wrong, consider the case where a = 5, b = 8. After these assigments, we expect that a = 8, b = 13 (since 13 is the next Fibonacci number). However:

// a = 5, b = 8
a = b
// a = 8, b = 8
b = a + b
// a = 8, b = 16

which is obviously wrong.

If you're using ES6 (which you're very likely to do, since you're already using generators), you can actually write this similar to the Python statement by using array destructuing:

[ a, b ] = [ b, a + b ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.