简体   繁体   中英

How to create a Javascript generator to calculate the Fibonacci sequence?

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!

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 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM