All right, so hopefully you gave that challenge a good go and you managed to get the correct solution
abiding by all of the rules. Now the rules are there because we want to test your understanding of
variables.
And this particular exercise is something that you often see in coding interviews where they're trying
to test a candidate's thinking process, how they go about reasoning, using code.
So let's go through the solution together and see if you got it right. Now, as I mentioned previously,
we're not allowed to change any of the existing code
so the solution can't just be us changing this from 3 to 8 and that from 8 to 3.
This is wrong.
The second rule is we're not allowed to type any numbers,
so we can't just simply say, well, a now equals 8 and b now equals 3.
This is also wrong because it breaks the second rule.
Now, the last rule is we're not allowed to re-declare the variables
a and b. So finally, we can't say a = "8" and variable b = "3".
In this case, we've actually just created two new variables
and you can see we're getting a little warning on the side and it says that "a is already defined, b
is already defined" up here, so we can't re-declare them.
So given all of those constraints, how do we arrive at the answer?
Well, as I showed you earlier on, it's really useful to think about it in terms of real objects to
understand the logic.
Now, we said that we've got two variables, right? a and b, and we can imagine them as two buckets;
bucket a and bucket b.
Now a contains sand and b contains water.
How would we switch the contents of the buckets around so that a holds water and b holds sand?
Well, if this was a real-life situation, then the simplest way is probably just getting hold of another
bucket,
right?
And this is exactly the same solution that we're going to implement using code as well.
So always remember that coding is not the hard part, remembering what the semicolons are, the equals
sign,
this is all very easy,
the difficult part is getting the logic in the first place.
So let's create this variable c and let's get it to hold some of the content that's currently inside
a.
And now that a is freed up, we can use a to hold what's inside b and finally we can get b to hold what
is inside c, which is what was previously inside a.
So now let's go ahead and test our code.
So I'm just going to copy these three lines and I'm going to paste it inside here.
And when I hit run, you can see that now a is 8 and b is 3.
And just these three lines of code allowed us to do the bucket switch.
And if we click check solution, then everything should pass and we should be allowed to continue.
So how did you get on with this challenge? If you got really stuck and you don't understand why the solution
is the way it is, then I urge you to just think about the buckets.
Think about how you would do it in real life,
what is the logic of switching the contents between a and b around when you have a bucket c and then
the code is going to become a lot more easily understood.
So if you need a moment, pause the video and have a think about it,
and then once you're ready, head over to the next lesson.