Loops, Arrays and Iterations…

Week 2 was certainly more difficult, there was a lot more material. Not unmanageable, but at times it felt a little overwhelming. I feel I came out with a good understanding of a number of different things – I’ll go into further detail below.

In the past, when working with JavaScript, looping really gave me headaches. I just didn’t understand them. I have learned this past week that it really comes down to just looking at it as a flow chart and visualizing where you want your code to go and to plan ahead with the end goal in mind!

Let’s look at some examples from the past week, there certainly are more than can be summarized in this one blog post, so look forward to more updates this week.

– Loops –

We worked with while loops and .times loops. It may seem a little daunting at first but let’s break down a quick example of a while loops…

screenshot-at-jan-23-16-13-53

In this we are trying to do something pretty basic. We prompt the user to give their name and favorite number, then we set up a counter that will 0utput numbers from 1 to the number chosen by the user. Simple, right?

So First,

Our prompt, puts. In Ruby, puts will print a line of text to the screen and then jump to the next line, as opposed to print, which will just print the text but not jump a line at the end. puts is shorthand Ruby for “Put String”.

Second,

We set a variable to house the user’s input. In Ruby this is done with gets.chompthis means that fav_num_str is going to be equal to whatever the user inputs in the terminal.

Third,

We make a NEW variable and name it fav_numand make THAT equal to whatever fav_num_str is…EXCEPT now we are modifying the class of that variable such that whatever the user inputs we will command the computer to convert it to an INTEGER using the .to_i method. This has a host of similar “siblings” like .to_s (String) or .to_f (Float, which is any decimal number like 5.25).

This means, for instance, if the user inputs 5.0, the computer will convert it to 5. If they type house it will convert it to ZERO, but that is for another post.

Moving on we set a counter to 0, and say that while the counter (Which at this point still equals 0) is < the value inputted by the user we will execute the following code.

We then add 1 to the counter total, 0 - now bringing the counter value to 1. Then we puts the counter (In this case 1 – so it will print to the screen: 1), then the loop ends, BUT ONLY IF when it gets back to the top of the loop, counter is GREATER THAN our new variable fav_num which is equal to the USER INPUT VALUE.

Check out these resources:

– Operators –

There are also, different types of operators when working inside your loops. Here is a list of different operators you will likely encounter depending on the problem you are working on:

ruby operatros.png

– .each method –

Now, what if we have an array (ex. time_table = [1,2,3,4,5,6,7,8,9,10]) and we want to print each value in the array in order? One way is to use an .each method. It is a more streamlined and hassle free version of the while loop. It would work something like this…

screen.png

And it really is that simple. You name your Array (in this case) and attach to that .each , in doing so, you will now run through EACH item in your array. Get it? Each. From there you open and close your LOOP with the do/end syntax. Now, you may be wondering, what the hell is that |num|? Well, the syntax for this method states that we identify what we are running through. So, in the case of .each methods, it wouldn’t matter if |num was |hippo| or |basketball|, just so long as when we referenced it later in our code block, it matched with whatever we initially called it as. It is the identifier of each iteration of the array. so when it says puts num, it will cycle through each item in the array and puts num or 1 then 2 then 3 and so on.

Check out these resources:

It can seem difficult at first but it gets easier especially as you begin creating random loops, phrases, etc in your free time. Try to do things yourself, do all of the examples provided to you from whatever source you are learning from but then apply that knowledge to just create random things that you aren’t really going to use for anything.

The point is to burn the capabilities of these functions and methods into your head! So get going, time’s a wastin’!

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s