Tell us what’s happening:
This test wants me to destructure an array, but an array does not exist. I’m assuming this means I’m supposed to put a and b into an array myself.
I do this in the same line I am destructuring. Is this not allowed for some reason? Why does this lesson want me to destructure an array that doesn’t exist? My code matches the code in the instructions exactly but is clearly failing at some point, but there isn’t enough information in the lesson to explain why.
Your code so far
let a = 8, b = 6;
(() => {
"use strict";
// change code below this line
const [a, b] = [b, a];
// 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 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0.
Edit: Replacing const with var does not work for some reason
let a = 8, b = 6;
(() => {
"use strict";
// change code below this line
var [a, b] = [b, a];
// change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8
fails
Final edit: There should be no declaration at all.
Because a and b already exist and should be global, do not use the const keyword to declare them. The task here does not actually create an array. Instead you’re containing using the variables in (temporary) arrays to be able to use destructuring assignment.
There’s no special concept of “temporary arrays”. Those are just arrays in variables that don’t have a very long scope, thus they’re temporary.
In this case, I don’t see why you’re using an IIFE. The purpose of those is to create an isolated scope, which is exactly what you do not want for this challenge. Remove the IIFE and just write the swapping code inline without any var/const/let.
the IIFE (and the thread) are from last version of the curriculum, when it was necessary to create a closed scope for the strict mode to be used. as for now the challenge has been changed and no more unnecessary IIFE