Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructuring assignment in while loop in ES6 function doesn't propogate out of loop?

I was implementing a simple GCD algorithm in ES6 (through node-esml) and came upon (to me) strange behaviour with updating values of variables inside a while loop. This code works fantastically:

function gcdWithTemp(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    temp = r
    r = rdash - q * r
    rdash = temp
  }
  return(rdash)
}
console.log(gcdWithTemp(97, 34))

Returning the expected answer of 1. However, if I remove the temporary variable and instead use destructuring assignment to try and achieve the same results:

function gcdWithDestructuredAssignment(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    [r, rdash] = [rdash - q * r, r]
  }
  return(rdash)
}
console.log(gcdWithDestructuredAssignment(97, 34))

It never completes, further debugging shows that r will always have the first value assigned to, x. It seems that these two implementations should be identical? see Swapping variables

I've also tried with using var instead of let to no avail. Am I drastically misunderstanding the point of destructuring assignment or missing something subtle? Or is it a bug?

like image 704
Steve Cockram Avatar asked Sep 28 '16 12:09

Steve Cockram


People also ask

What is Destructuring assignment in es6?

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

What are default values in Destructuring assignment in JavaScript?

Default values in destructuring assignement only work if the variables either don't exist or their value is set to undefined . Any other value, including null , false and 0 , bypasses the default values in the destructuring statement.

What is Destructuring assignment?

Destructuring Assignment is a JavaScript expression that allows to unpack values from arrays, or properties from objects, into distinct variables data can be extracted from arrays, objects, nested objects and assigning to variables.


1 Answers

That's a problem not with destructuring assignment, but with ASI (automatic semicolon insertion). These two lines:

q = Math.floor(rdash / r)
[r, rdash] = [rdash - q * r, r]

in practice mean this:

q = Math.floor(rdash / r)[r, rdash] = [rdash - q * r, r]

which obviously is not what you meant. To fix that, add a semicolon in front of [:

function gcdWithDestructuredAssignment(x, y) {
  let [r, rdash] = [x, y]
  while (r != 0) {
    q = Math.floor(rdash / r)
    ;[r, rdash] = [rdash - q * r, r]
  }
  return(rdash)
}
console.log(gcdWithDestructuredAssignment(97, 34))

Of course you can add the missing semicolon at the end of the previous line instead (q = Math.floor(rdash / r);), but since you generally don't use semicolons, I assumed that you're using npm coding style.

like image 122
Michał Perłakowski Avatar answered Sep 28 '22 09:09

Michał Perłakowski