# Not understanding how to set this up

Hi. I read the explanation for deconstructing an array and it make sense. But in this operation, the starting statement doesn’t seem to be an array (according to what I’ve learned so far). I’m supposed to reassign the values somehow, but the article didn’t go over an example of that operation either.
I tried making an array like this `const [a, b]`, but it says `a` and `b` have already been declared. hmm… I tried some other things but none of them would have reassigned the values. I’m kind of stumped. Can someone give me an idea of how to get started?

Tell us what’s happening:
Describe your issue in detail here.

Your code so far (this is just the sample problem, I already deleted my attempt.)

``````
let a = 8, b = 6;
// Only change code below this line

``````

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

Challenge: Use Destructuring Assignment to Assign Variables from Arrays

It was an accident. I just tried to edit my post and it created a duplicate. I don’t know why.

Can you show us what you’ve tried?

But in this operation, the starting statement doesn’t seem to be an array (according to what I’ve learned so far).

Correct. The point is that you would put it in an array literal and then use destructuring to extract them.

I tried making an array like this `const [a, b]` , but it says `a` and `b` have already been declared

Right, in the given example, it declared those variables. But they’ve already declared the variables here so we just need to assign them new values, not redeclare them.

Can someone give me an idea of how to get started?

This is the example given:

``````const [a, b] = [1, 2, 3, 4, 5, 6];
``````

You are going to do something similar, with two big differences. You’re not going to declare your variables here, and you will have an array literal in the right hand side of the assignment, but it won’t look quite like that, it’s going to use some variables.

The end result is going to look a little strange. But once you understand what is happening, it makes sense.

Show us what you’ve tried an maybe we can guide you better.

``````const [a, b] = [1, 2];
console.log(a, b);  //logs 1 and 2
``````

You already see the pattern here i suppose. The array on the left side mimics the indexes of the array on the right side. ‘a’ which is on index 0 is assigned the value on index 0 on right side, which is equal 1, ‘b’ takes the second index. Instead of numbers, we can use variables too.

``````let c=5
const [a, b] = [c, 2];
console.log(a, b);  //logs 5 and 2
``````

here i put value of 5 to c, then i assign ‘a’ to be equal ‘c’(which is 5). The challenge requires us to swap the values of a and b, so a is assigned the value of b and the opposite. Think of how you would do that using this destructuring technique
PS: Like you stated, ‘a’ and ‘b’ have been already declared, we dont want to re-declare them, but to assign new valeus to them. How do we assign new value to a variable? We certainly dont put `const` or `let` in front of it, which are operators to declare a new variable. We simply refer to them and make them equal a given value. Assigning a new value to a variable vie destructuring, same rule applies, we do not use `const` etc.

It is also worth giving the MDN article on destructuring assignment a read if you have not. Just note that the swapping variables example is very much a spoiler for this challenge.

2 Likes

thank you. I think I did accidentally read too far down the page and see the spoiler. but @Sylvant 's example was also really helpful and already had me on the right track I think.

Haven’t tried this solution yet, but I’m thinking it’s going to be something like:

`[a, b] = [b, a]

Yeah, that seems about right. But why guess? Just try it out.

Well, if you are ever in doubt run the code and log it out. Or in this case, run the tests.

That is what is so great about code, running it will keep you honest. 99.9% of the time if something doesn’t work as expected you, or the person that wrote the code your code is running on, made a mistake.

Here is an interesting video about one other very unlikely 0.1% (probably a lot more zeros to be exact)

2 Likes

Yes, I have been using `console.log` a lot to test solutions. only problem is I’m only keeping track of what I have already tried in my head, so unfortunately when I came here to ask for help I wasn’t recalling my previous efforts. I should probably write code with pen and paper from time to time, that’s always been a good learning strategy for me. I’m gonna check out that video a little later. Thanks again!

oh, and btw I just got home and tried my solution. It works! I was responding to this thread from my phone earlier. Thanks, everyone.

1 Like

this video was recommended to me planty of times by youtube but i always thought the image to be a clickbait so i never opened. Now i see the video actually offers some interesting information. Its hard to sift good from bad info nowadays ^^

I know this is going off topic a bit, but Veritasium videos are always legit. He has recently started making his videos more “clickbait” with titles and images just because that’s how YouTube works, but the content is always good. Its more general science stuff then computer related, but his channel is worth checking out if you liked that video.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.