All language subtitles for 116 Functions Part 1 Challenge - The Karel Robot.en_US

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now, as a challenge, I want you to write your own functions, but not inside the Chrome Developer Tools.

I want to introduce you to something called Karel the robot.

And Karel is a very simple robot that takes very simple instructions.

So let's first change the world to a five by five to make it a little bit smaller and easier to work

with.

Now the next thing that you'll notice is that you have this thing called function main.

And in this case everything that you put into here will get executed when you click the run button.

And Karel the robot, as you can see here, is able to take a certain number of commands, including move,

which moves it forward in the direction it's facing, turn left,

put down a beeper, or pick up a beeper.

So those are the basic commands that we're going to work with.

And let me just demo some of these to you.

So, if we call the function move on Karel, you can see it moves by a single space, and we can obviously

repeat this a few times.

So now if we reset and we hit run, Karel will move by three spaces, because we wrote the move command three

times.

So everything you place into this function main will get called when you hit run.

But you can create more than one function.

So, for example, I know that I can make Karel turn left by simply writing turn left, run, and she turns

left.

Now if I wanted her to go around in a circle then I can simply create a function called

goInCircle, and inside this function I can say maybe move one step,

turn left, then move, then turn left, then move.

And now I can go into this function main and I can say goInCircle, calling that function that I just

created down here, which of course will execute all of the code that's inside the curly braces.

So now if I reset and I hit run you can see that Karel goes in a half circle.

So if I want her all the way back to the beginning then I can simply call this function again, making

sure that I spelt it exactly the same way as I did when I created that function.

So now, if I reset and hit run, you can see that it turns a full circle, and I can write as many of these

as I like in order for Karel to run my code repeatedly.

So, as a challenge, I want you to write a little bit of code that moves Karel all the way to the right

corner on a five by five world.

So, inside the resources of this lesson, you'll find a link to the Stanford Karel.

So I want you to set the world to five by five,

delete what's currently inside function main, and to write some code that gets Karel from the bottom

left corner to the top right corner,

remembering that you can always take a look at the reference to see what commands

Karel is able to use.

So pause the video now and give that a go.

All right.

So that shouldn't have been too difficult.

And there's many many ways of solving this challenge.

So it doesn't matter which way you used as long as Karel ended up over here when you hit run.

So, one way of doing this would be getting Karel to go one, two, three, four, four steps forward.

So that would be move four times

and then to turn left and then move four times again.

So now if we hit run you can see Karel ends up in the top right corner.

Now some of you might have realized that move is repeated here.

So you might have created a function that's called maybe moveFourTimes,

and inside this function you've placed four of these move commands.

So now instead of all this repeated code, you can say moveFourTimes here, and also moveFourTimes

here.

So this does exactly the same, as we can see if we reset it, but it's now a little bit shorter and a little

bit less repetitive.

So using this principle of keeping your code dry we can use functions to do that.

It allows us to remove repetition into these modules of code which makes our code shorter and allows

us to identify problems

if it does occur.

So, for example, if we’ve accidentally written the code wrong and Karel only moved three steps each time,

then we would know that we can identify the function that's meant to move four times, and figure out

what the problem was.

So the next thing I want you to use is the command putBeeper.

So Karel is able to put down this thing called a beeper, which is kind of just like a square.

And I want you to write some code that commands her to put down the beeper in a specific pattern.

So the first slightly easier challenge is, can you get her to put a diagonal line of beepers all the

way from the bottom left corner to the top right corner.

So that should be five beepers in total and I want you to try and write your code in the least repetitive

way possible using functions.

So pause the video now and see if you can complete this challenge.

OK.

So let's break down the problem. Essentially, Karel will start in the bottom left corner and she can move

forwards, turn left, move,

put down beeper.

So, one way of doing this would be writing move forwards, turn left, move forwards, put beeper, turn right,

move forwards, turn left, move forwards, and put beeper.

So, if we run our code as is, you can see that she's begun on her journey, but you might also notice that

there's a lot of repeated code in here, especially something like,

for example, move, turn left, move, put beeper, move, turn left, move,

put beeper.

So there's a lot of things that are repeated.

So what if we could put all of that into a function? So, we can create a function diagonalMoveAndBeeper.

Now of course you can name your functions whatever it is that makes sense to you but in this case I've

just called it diagonalMoveAndBeeper, because I'm going to get it to move diagonally and then put

down a beeper.

And that involves moving, turning left, moving, putting down beeper, and turning right.

So if I take this chunk of code and put it into my function then instead of calling all of these steps

I can simply say diagonalMoveAndBeeper, diagonalMoveAndBeeper.

And if we do this four times then you can see that Karel will go all the way to the end, and the only

one that's missing is the one in the beginning, the putBeeper before we do any of these other diagonal

moves.

So if we hit run now we've managed to solve our solution by getting Karel to repeat the diagonal move

and put down beeper function.

Now you might have solved this challenge in a variety of different ways and that's fine as long as you

got Karel to perform this functionality.

Then you have succeeded in completing the challenge.

Now for the final optional challenge, if you want,

I'd like you to challenge yourself to see if you can get Karel to create a chessboard pattern like

what we have here,

so alternating tiles of beepers essentially.

So this is completely optional.

It's just a bit of fun

and to test your understanding of functions. I will post the code of the solution in the resources section

of this lesson, so you can check it

once you're done trying out the challenge. In the next lesson I want to talk about a more advanced form

of functions and those are functions that allow you to specify an input.

So for all of that and more, I'll see you on the next lesson.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.