0

[a,b] = [b, a+b] here not work , a b always 0 and 1.

If use a temp variable to swap the value ,that works.

function fibonacciSequence() {
  let [a, b, arr] = [0, 1, []]
  while (a <= 255) {
    arr.concat(a)
    [a, b] = [b, a + b]
    console.log(a, b) // always 0 1
  }
}
console.log(fibonacciSequence())

2
  • what is your desired output? Commented Jul 18, 2019 at 1:01
  • What do you expect arr.concat(a)[a, b] to mean? Commented Jul 18, 2019 at 1:02

2 Answers 2

6

The problem is that Automatic Semicolon Insertion isn't doing what you expect. It's not adding a semicolon between

arr.concat(a)

and

[a, b] = [b, a + b]

so it's being treated as if you wrote

arr.concat(a)[a, b] = [b, a + b]

Add all the semicolons explicitly and you get a correct result.

function fibonacciSequence() {
  let [a, b, arr] = [0, 1, []];
  while (a <= 255) {
    arr.concat(a);
    [a, b] = [b, a + b];
    console.log(a, b); // always 0 1
  }
}
console.log(fibonacciSequence())

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the below function as well:

    function fibonacciSequence() {
      let [a, b] = [0, 1];
      while (a <= 255) {

       b = a + b;
       a = b - a;
       console.log(a,b);
      }
    }
    fibonacciSequence();

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.