DEV Community

Piyush Acharya
Piyush Acharya

Posted on

2648. LeetCode's Generate Fibonacci Sequence - JS Solution Beats 56% in Memory

Code

/**
 * @return {Generator<number>}
 */
var fibGenerator = function*() {
    let a = 0, b = 1;
    yield a;
    yield b;

    while (true) {
        let c = a + b;
        a = b;
        b = c;
        yield c;
    }
};

/**
 * const gen = fibGenerator();
 * gen.next().value; // 0
 * gen.next().value; // 1
 */
Enter fullscreen mode Exit fullscreen mode

Top comments (0)