Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers- explain pls

can you guys explain to me what’s happening on that line of code
a = [b, b=b+a][0]; because i’m still new to javascript . and thats why i couldn’t understand what that line of code is doing ??

Your code so far


function sumFibs(num) {
var a = 1;
var b = 1;
var s = 0;
while (a <= num) {
  if (a % 2 !== 0) {
    s += a;
  }
  a = [b, b=b+a][0];
}
return s;
}
console.log(sumFibs(4));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

That’s really convoluted way to assign at the same time a = b and b = b + a. To a is assigned first element (with index 0) of the array [b, b=b+a], while b is being reassigned to b = b + a.

This can be written more clearly in few ways. For example:
[a, b] = [b, b + a];
or with using another variable:

let temp = a
  a = b
  b = b + temp;
2 Likes

thank you for explaining that !