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.
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?
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
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
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”
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