What is the output of the following? “`js function* fibonacci() { let [a, b] = [0, 1]; while (true) { yield a; [a, b] = [b, a + b]; } } const fib = fibonacci(); const result = Array.from({length: 5}, () => fib.next().value); console.log(result); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js function* fibonacci() { let [a, b] = [0, 1]; while (true) { yield a; [a, b] = [b, a + b]; } } const fib = fibonacci(); const result = Array.from({length: 5}, () => fib.next().value); console.log(result); “`

Key points

  • The Fibonacci function uses a generator to yield values.
  • Array.from creates an array from the generator output.
  • The Fibonacci sequence starts with 0 and 1.
  • The next values are calculated by adding the previous two values.
  • The correct output is [0, 1, 1, 2, 3].

Ready to go further?

Related questions