Use-destructuring-assignment-to-assign-variables-from-arrays

Tell us what’s happening:

I don’t know how to Destructuring the array and change the value;because of one parameter passed in.

Your code so far



let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  // change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:

Read the challenge carefully. Look for the part where it mentions which letter is assigned to a certain number.

Swapping values is easy as well.

If you have 2 cars and I have 1 car, how would you make it so I had 2 cars?

What’s the mathematical way to give something a value?

2 Likes

Thanks so much .I have resolved it.First of my mind is the parameter passed in.maybe I should calm down and replace thinking.:astonished:

1 Like
[a,b]=[b,a];
16 Likes

How come you don’t need const before the [a,b]? Is it because they are already defined above using let?

2 Likes

The part I can’t get my head around is console.log tests. It seems like [a,b]=[b,a]; is making a new array but without a name. Is there such a thing as an anonymous array? and if so, shouldn’t you have to say anonArr[0] to get the value?

1 Like

I never got the reference error when trying to use const.

I feel like this new ES6 portion is a little hard to get used to, since all the previous exercise problems followed the examples more closely.

15 Likes
let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
const c = a;
a = b;
b = c;
  // change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8


[b, a] = [a, b] worked for me. Was this not the correct response?

1 Like

Yes, that is the correct way to swap the value of variables using destructuring a assignment. One line, no temporary variable

1 Like

you did not use destructuring -assignement

Hi,

maybe I missed something in the previous lectures, but what does the part below stand for in the exercise code?

(() => { })();

3 Likes

Solution:
let a = 8, b = 6;
(() => {
“use strict”;
// change code below this line
[a,b] = [b,a];
// change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8

5 Likes

Have you figure it out? I would also like to understand this.

1 Like

It’s an IIFE (immediately invoked function expression)

(
function( ){
//do stuff
})
(); //calls it straight away.

The call can be written after final } like so:

(function( ){
//do stuff
}
());

From what I’ve seen it’s down to preference of where you choose to shove the call. It works both ways.

3 Likes

Hi, thanks for the explanation. I also found this one from stack overflow helpful:

I still think there is something off with the sequence in the curriculum.
The ES6 stuff seems pretty complicated and w/o any explanations like “What is an IIFE”, while the previous part “Basic Java Script” as super basic. I am missing some lectures with a difficutly level between “For-Loops” and “Destructuring assingments and IIFEs” :slight_smile:

11 Likes

Ok so i could not understand my mistake for ages , but then I released that I was declaring a
const [a,b] = [b,a]… This was the problem, the variables i wanted to change have been declared in the global scope with let, but I was assigning the values to new const I had created in the body of the function.

###########################################
the solution is to just assign the variables to each other without any new declarations like so
let a = 8, b = 6;
(() => {
“use strict”;
// change code below this line
[b,a]= [a,b]
// change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8

4 Likes

//This works fine:

let a = 8, b = 6;

[a,b]=[b,a];

console.log(a); // should be 6
console.log(b); // should be 8

// what’s the point of the immediately invoked function expression?
// Maybe it’s so you can include “use strict”?

Found this on MDN. Clearly explains how to swap!
Two variables values can be swapped in one destructuring expression.

Without destructuring assignment, swapping two values requires a temporary variable (or, in some low-level languages, the XOR-swap trick).

var a = 1;
var b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

No need to declare another variable in the function. The arrays can swapped if assigned to each other.

1 Like

I did this
const [b,a] = [a,b]; which failed,
not

[a,b]=[b,a];

Not sure why.

1 Like