Swap variable values in Use Destructuring Assignment to Assign Variables from Arrays challenge

Tell us what’s happening:
I am trying to swap the values of the variables, but I cannot manage to make it work, which I find strange because the HINT provided for this challenge also mentions the one I am using should be the solution, anyone could spot what I am doing wrong please?

Your code so far


let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  const [b,a] = [a,b];
  // 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.14; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays

By using const inside the function, you’ve created new variables inside the local scope, but the a and b outside the function remain unchanged.

1 Like

Remember that let allows you to freely reassign the variable’s value.

let someNum = 5;
console.log(someNum); // 5

someNum = 6;
console.log(someNum); // 6

function changeSomeNum() {
  someNum = 7;
}

changeSomeNum();
console.log(someNum); // 7

I am still unclear :frowning:

Your code is basically doing this:

1 let a = 8, b = 6;
2 (() => {
3   const innerB = a;
4   const innerA = b;
6 })();
7 console.log(a);
8 console.log(b);

By using const you’ve created two new variables inside the function assigned them the values of a and b. But the console logs on line 7 and 8 refer to the variables created on line 1, and those variables remain unchanged.

Sorry I keep replying, but I’m trying to find the right explanation because this is an important concept.

In JavaScript there are these things called scopes. At the top level of your code is the global scope. Everywhere in the code can access functions and variables defined in the global scope. On the other hand, the code written inside a function definition is part of its own local scope. Inside the local scope of a function, you can access the both the local and global scope. But the global scope cannot access local scope.

// Global scope
const globalVariable = 'I am a global variable';

function foo() {
  // Local scope
  const localVariable = 'I am a local variable';

  console.log(globalVariable) // I am a global variable
  console.log(localVariable)  // I am a local variable
}

// Global scope
console.log(globalVariable) // I am a global variable
console.log(localVariable)  // ReferenceError: localVariable is not defined

As you can see in this example, console logging inside the function correctly prints both variables, but if you try to do the same in the global scope you get a reference error because localVariable only exists inside the function foo.


So let’s take a look at the problem again and find the parts that are globally scoped and those that are locally scoped.

// Global scope
let a = 8;
let b = 6;

(() => {
  // Local scope (inside a function definition)
  const [b, a] = [a, b] 
  // This line makes two local variables, 'a' and 'b'.
  // Those local variables are NOT the same as the global variables 'a' and 'b'.
  // The global variables 'a' and 'b' remain unchanged.
  // local a: 6
  // local b: 8
  // global a: 8
  // global b: 6
})();

// Global scope
console.log(a); // Finds the global variable 'a' and logs its value: 8
console.log(b); // Finds the global variable 'b' and logs its value: 6

The reason you’re not passing the tests is because you have to change the global variables inside the function’s local scope. That can be done simply by removing the const keyword.

2 Likes

@colinthornton thanks for taking the time to explain, I did get it in the end :slight_smile:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like