In this first exercise, we are doing to learn how to count up and down using
Javascript. To do this, we are going to learn about the for
loop,
which tells the computer to repeat something.
A for
loop looks like this:
for (var i = 0; i < 10; i = i + 1) { print(i); }
Let's copy that example code and run it to see what happens.
Once you have that working, let's try to understand each piece of this code.
The first word for
tells the computer that we want do to a for
loop.
for (var i = 0; i < 10; i = i + 1) { print(i); }
The parentheses (
and )
contain the instructions on
how it should repeat.
for (var i = 0; i < 10; i = i + 1) { print(i); }
First, we need to tell the computer how to setup our for
loop.
The var
tells the computer that we want to have a new variable. A
variable holds a number that we can change.
for (var i = 0; i < 10; i = i + 1) { print(i); }
The i
is the name of the variable, and we can use this name to change
or use the variable later
for (var i = 0; i < 10; i = i + 1) { print(i); }
The =
tells the computer to put a value into our variable. Here,
we are putting 0
into our variable.
for (var i = 0; i < 10; i = i + 1) { print(i); }
The ;
tells the computer that we are done telling it how to
setup our for
loop.
for (var i = 0; i < 10; i = i + 1) { print(i); }
Now we want to tell the computer how many times to repeat. The i
is the same variable we made above. The <
means "less than" and
10
is what we want i
to be less than. So we are telling
the computer that we want to keep repeating as long as i
is less than
10
.
for (var i = 0; i < 10; i = i + 1) { print(i); }
Now we want to tell the computer how to keep track of how many times it has
repeated. We are using our variable i
again and we are using
=
to set its value. This time, we are setting it to the value of
i plus the number 1.
for (var i = 0; i < 10; i = i + 1) { print(i); }
For example, if the varibale i
has the value of 0
in it,
after we do i = i + 1
, the variable i
will now have the
value 1
in it. If we do i = i + 1
again, i
will have a 2
in it. If we do it another time, i
will
have a 3
in it.
So if you look at everything inside the parentheses in the for
loop:
for (var i = 0; i < 10; i = i + 1) { print(i); }
it is saying:
I want to use a variable named 'i', start it at 0, keep looping while 'i' is less than 10, and each time I repeat, add 1 to i."
Now we move on to what we want to repeat. That is all contained inside the
curly braces {
and }
. This is called a "scope".
Everything between these curly braces will be run every time we repeat.
for (var i = 0; i < 10; i = i + 1) { print(i); }
Finally, inside our scope, we want to call a function named print
. A function
is another piece of code. In this case, print
will output whatever you
call it with.
for (var i = 0; i < 10; i = i + 1) { print(i); }
The print
function needs to know what to output. We can do this by giving
it "arguments". "Arguments" are given to the function by putting parentheses (
)
after the function and telling the computer what variables you want to
give to the function.
for (var i = 0; i < 10; i = i + 1) { print(i); }
In this case, we are giving the variable i
to the the print
function.
for (var i = 0; i < 10; i = i + 1) { print(i); }
Finally, the ;
tells the computer we are done with the line.
for (var i = 0; i < 10; i = i + 1) { print(i); }
I hope you understood all of that. If not, please ask Daddy or Mommy questions! Okay, now for your challenges!!
Write a for
loop that prints all the numbers from 0 to 100, including
the 0 and 100.
Write a for
loop that prints all the numbers from 100 down to 0, including
the 0 and 100.
Write a for
loop that prints all the numbers from 0 to 100, counting by
2