We have an array
and
we need to print the
double of each item
in this array.
We can do this in so
many ways.
First let's see how we do this
with the for
loop. There are
two different ways that we can
use:
We can also use the for loop
a bit differently as shown below:
Using each is the best way
In JavaScript and some
other programming languages,
for loops are used a lot.
However, in Ruby the most
preferred way of iterating
through items in an
array is by using each
.
You can see that this is a
simpler approach to achieving
the same result.