Introduction to Programming

My goal is to do just 30 minutes a day on Team Treehouse, but when I started on this intro course, I couldn’t just stop.  So I finished the whole thing in a day. There are 4 parts to the Intro course: the “Basics,” Control Structures, Objects and Arrays, and Functions.

BASICS

Basics is the first part of the Introduction to Programming course. The first things that came up were the alert and prompt commands

Alert brings up a pop up box with a message in the browser.
Do you remember about 10 years ago when you’d go to some websites, especially MySpace pages, and you’d have a pop up box with something really annoying. Then you’d click “OK” and another box would come up. Then you’d keep clicking OK and getting more boxing laughing at you because you got yourself onto a page with seemingly endless alerts.

Prompt command brings up a text box for user input. You may have seen these on some websites that won’t let you use them until you type in your age.
Variables
They’re declared with word var, then the name of the variable comes next. After that, you’d put an equal sign and then you’d define your variable. Conventionally, variable names start with lowercase letters. If you’re using more than one word, you’d capitalize the second ones like this: var myName = Cerita;
The variable is called myName and it contains my name. Now, I can write something like alert(myName); and the alert box would just show ‘Cerita.’

Comments
A comment is a line in your file that doesn’t run as code. Say you’re testing out your code and you can’t figure out where the error is. You can comment out a line of code to keep it from running. You’d do that with two slashes: // or, to comment multiple consecutive lines, you’d use /* (write your comment here) */

 

 

CONTROL STRUCTURES

This section covered If statements and loops.
If statements are conditional. If certain conditions are met (i.e., the statement is true), then a certain action will be performed. You can write if statements alone, or, if there are multiple conditions, you can add else statements.
Real life example: If it’s Monday-Friday, wake up at 6AM. Else, sleep in til noon.
If it is Tuesday, go to your night class. Else, if it is Friday, go to dance class. Else, go home and do nothing.

Next, came loops.
First, there are while loops that loop through the code while a certain condition is true (While hungry, eat pizza).  Soon as the condition becomes false (Not hungry), the loop ends.

Then, there are for loops. They loop through code a specified number of times.
for (declaration before the loop; condition to run loop; action after the loop).
Think of the popular 99 Bottles of Beer on The Wall song and the initial for statement is stating the rules of singing the song. You start off with 99 bottles. As long as you have more than 0 bottles, you’ll keep singing the song. And every time you finish the song, you’ll subtract a bottle and start again.

for (var bottles = 99; bottles > 0; bottles = bottles – 1) {
alert(bottles + “bottles of beer on the wall!”)
}

ARRAYS

Arrays are a list of values. They can be all string, all numbers, a mix of the two. You can even have an array of arrays!
Items in an array are contained by two square brackets and separated by commas.

Instead of just explaining away what an array is, the instructor builds on the previous lessons. We created an array of friends (I just used his list), then had the console display each item in the array.

 

OBJECTS

Objects are basically exactly what they sound like…objects. But you can’t use a word in its own definition, so an object is a collection of attributes for one…object. You are a human object. You contain attributes like your eye color, height, and even your name. And every object has methods, or actions it can carry out. As a human object, you have methods like sleep, eat, and poop.

In JavaScript, you declare an object by creating a variable and assigning all of its properties (attributes) in the curly braces.

var human = {firstName:“Cerita”, lastName:“The Great”, eyeColor:“brown”, height:”5 foot 8″ };

To access the properites of an object, you’d call it like this:
human.firstName()

(In Ruby, these are called hashes.)

FUNCTIONS

Functions store behaviors of objects. Prompt and Alert are both functions, or behaviors, in JavaScript. There are lots of functions that are already part of JS, but you can also create your own.

 

Advertisements

Leave a Reply

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