WEBVTT 0 00:00.900 --> 00:06.630 All right, so hopefully you gave that challenge a good go and you managed to get the correct solution 1 00:06.930 --> 00:12.990 abiding by all of the rules. Now the rules are there because we want to test your understanding of 2 00:12.990 --> 00:13.620 variables. 3 00:14.070 --> 00:20.370 And this particular exercise is something that you often see in coding interviews where they're trying 4 00:20.370 --> 00:25.770 to test a candidate's thinking process, how they go about reasoning, using code. 5 00:26.340 --> 00:32.040 So let's go through the solution together and see if you got it right. Now, as I mentioned previously, 6 00:32.040 --> 00:34.280 we're not allowed to change any of the existing code 7 00:34.590 --> 00:40.940 so the solution can't just be us changing this from 3 to 8 and that from 8 to 3. 8 00:41.100 --> 00:42.240 This is wrong. 9 00:42.660 --> 00:45.480 The second rule is we're not allowed to type any numbers. 10 00:45.480 --> 00:51.330 So we can't just simply say, well, a now equals 8 and b now equals 3. 11 00:51.660 --> 00:54.750 This is also wrong because it breaks the second rule. 12 00:55.590 --> 00:58.890 Now, the last rule is we're not allowed to re-declare the variables 13 00:58.890 --> 01:07.710 a and b. So finally, we can't say a = "8" and variable b = "3". 14 01:08.190 --> 01:11.220 In this case, we've actually just created two new variables 15 01:11.220 --> 01:17.040 and you can see we're getting a little warning on the side and it says that a is already defined, b 16 01:17.040 --> 01:20.320 is already defined up here, so we can't re-declare them. 17 01:20.850 --> 01:25.920 So given all of those constraints, how do we arrive at the answer? 18 01:26.280 --> 01:32.730 Well, as I showed you earlier on, it's really useful to think about it in terms of real objects to 19 01:32.730 --> 01:33.990 understand the logic. 20 01:34.560 --> 01:40.530 Now, we said that we've got two variables, right? a and b, and we can imagine them as two buckets, 21 01:40.530 --> 01:41.490 bucket a and bucket b. 22 01:41.490 --> 01:44.930 Now a contains sand and b contains water. 23 01:45.360 --> 01:51.450 How would we switch the contents of the buckets around so that a holds water and b holds sand? 24 01:51.870 --> 01:58.260 Well, if this was a real-life situation, then the simplest way is probably just getting hold of another 25 01:58.260 --> 01:58.710 bucket, 26 01:58.710 --> 01:59.060 right? 27 01:59.490 --> 02:04.190 And this is exactly the same solution that we're going to implement using code as well. 28 02:04.620 --> 02:09.960 So always remember that coding is not the hard part, remembering what the semicolons are, the equals 29 02:09.960 --> 02:10.170 sign, 30 02:10.230 --> 02:11.620 this is all very easy. 31 02:11.910 --> 02:15.170 The difficult part is getting the logic in the first place. 32 02:15.690 --> 02:22.020 So let's create this variable c and let's get it to hold some of the content that's currently inside 33 02:22.320 --> 02:22.740 a. 34 02:23.730 --> 02:31.650 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 35 02:31.650 --> 02:36.060 is inside c, which is what was previously inside a. 36 02:36.600 --> 02:39.680 So now let's go ahead and test our code. 37 02:39.720 --> 02:44.220 So I'm just going to copy these three lines and I'm going to paste it inside here. 38 02:44.610 --> 02:49.020 And when I hit run, you can see that now a is 8 and b is 3. 39 02:49.260 --> 02:53.160 And just these three lines of code allowed us to do the bucket switch. 40 02:53.670 --> 02:59.010 And if we click check solution, then everything should pass and we should be allowed to continue. 41 02:59.670 --> 03:05.610 So how did you get on with this challenge? If you got really stuck and you don't understand why the solution 42 03:05.610 --> 03:09.500 is the way it is, then I urge you to just think about the buckets. 43 03:09.750 --> 03:11.940 Think about how you would do it in real life, 44 03:11.940 --> 03:18.450 what is the logic of switching the contents between a and b around when you have a bucket c and then 45 03:18.450 --> 03:21.790 the code is going to become a lot more easily understood. 46 03:22.350 --> 03:25.260 So if you need a moment, pause the video and have a think about it. 47 03:25.440 --> 03:27.690 And once you're ready, head over to the next lesson.