Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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 encoding 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
were not allowed to change any of the existing code.
So the solution can't just be us changing this from three to eight and that from eight to three.
This is wrong.
The second rule is we're not allowed to type any numbers so we can't just simply say well net a now
equals eight and b now equals three.
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 equals eight and b variable B equals three.
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 ready to find b is
ready 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 and Bucket B.
Now a contained son and B contains water.
How would we switch the contents of the buckets around so that a hold water and b hold 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 equal 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 a and b is three 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 a video and have a think about it and then once you're ready head over
to the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.