Showing posts with label Week 1. Show all posts
Showing posts with label Week 1. Show all posts

Thursday, April 7, 2011

Chapter 2 Notes

Commands 
You can recognize commands because they have parenthesis

Web Specific
alert()
document.write()


Universal to Javascript
isNaN() - checks to see if a particular value is a number or not


Types of Data
 Numbers
document.write(5 + 15);
whole numbers, decimals, negative


String
can be in double or single quotes
"This isn’t fair" (us double quotes to enclose a string with a single quote inside it)
'He said, "Hello."' is a valid string (how to include quotes in a string)
"He said, "Hello.\ "" (use the \ - an escape character - to tell it to include that quote in the string)
'He said, "This isn\'t fair."'   (you have to use the escape character to make that phrase work)
Boolean
you can use boolean value to check if a user did something or not, such as did they supply an email address - true or false


Variables 
Stores information for later use
 
Naming Convention for Variables
Starts with a letter, $, _
Contain alphanumeric characters and $ and _ (No spaces)
Case-sensitive
Don't use special keywords like alert, break, catch, for, new, this, void.

You  can create multiple variables at once like this:
var playerScore, gameRound, playerName; 
And you can declare and store multiple pieces of information inside like this:
 var playerScore=52, gameRound=1, playerName='Bob';


Basic Math

+ - / *

var price= 5, itemsOrdered=2, totalCost=price*itemsOrdered;


make sure to use parenthesis when having multiple mathematical operations, for example:
(4 + 5) * 10   (this returns 90)
4 + 5 * 10   (this returns 54)
what ever in in the parenthesis happens first.




Combining Strings  
Combining strings is called concatenation

var firstName = 'Bob'; 
var lastName = 'McPoopface'; 
var fullName = firstName + lastName;  

This gives you BobMcPoopface

var firstName = 'Bob'; 
var lastName = 'McPoopface'; 
var fullName = firstName + ' ' + lastName; 

This gives you Bob McPoopface (with a space)

Combing Numbers and Strings 
Automatic Type Conversion
Whenever you use the + operator with a string value and a number it concatenates it to a string


var numOfPoops = 900; 
var message='Your dog ate ' + 'numOfPoops' + 'poops today.'; 
returns: Your dog ate 900 poops today.


Sometimes converting is bad.
For example:
var numOfCatPoops = 25; 
var numOfDogPoops = 5; 
var numOfPoops = numOfCatPoops + numOfDogPoops;   
var message='Your dog ate ' + 'numOfPoops' + 'poops today.';  
returns: Your dog ate 255 poops today. (instead of 30)




To prevent this:
(Two ways to convert)
1. Add + to the beginning of the string with the number:
var numTotal = +numStuff   


2. Number(variable)
var numTotal = Number(numStuff) + numMoreStuff  





Changing the Values in Variables

var score = 0;
score = 100;

var score = 0;
score = score + 100;

Add 100 to what ever value is currently store in "score".
You are preforming a mathematical operation on the value of a variable and then storing that value back into the variable.

Shortcuts:
score += 10;
score = score + 10;

score -= 10;
score = score - 10;

+=   -=   *=    /=  

You can also use += for strings:

var name = 'Bilbo';
var message = 'Hello'; 
message = message + ' ' + name;   >>becomes>>  message += ' ' + name;


Adds or subtracts 1
score++
score--
 ++   --



Arrays
You can store more than one value in a single place, so you don't have to keep creating tons of variables.


var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];  


var listOfStuff = [];  
an empty array that can be supplied with information later ( [] two brackets)

Inside of an Array you can store:
Numbers
Strings
Boolean
Arrays
Other Objects
(you can store any combination of from this list)


Getting Items Out of the Array
same as ActionScript, the items have position numbers/index numbers

var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July'];
alert(months[2]);  
this would return Mar

Change the Value of an Item
months[0] = 'January';  
now Jan is changed to January


Get the Last Position of the Array
months[months.length-1];  
gets the total number of positions and subtracts 1
so, for months this would be 6 (since I have only listed 7 months)

When working with loops you might want:
var i = 0; 
alert(months[i]);

Adding Items to the End of an Array

var properties = ['red', '14px', 'Arial'];  

Three ways:
properties[3] = 'bold'; 
properties[properties.length] = 'bold';
properties.push('bold');  



Adding Items to the Beginning of an Array
var properties = ['red', '14px', 'Arial'];   

properties.unshift('bold');

you can push and unshift multiple values like this:
properties.push('bold', 'italic', 'underlined');
properties.unshift('bold', 'italic', 'underlined');  



push() and  unshift() are methods
they return a value, the total items in the array

for example:
var p = [0,1,2,3]; 
var totalItems = p.push(4,5);
totalItems is 6
 

Creating a Queue / FIFO
playlist.push('Dumb Song');
nowPlaying = playlist.shift();

This cycles through, you add a song to the end of the list.
Then you play the first song on the list, by removing it and storing it inside nowPlaying

Deleting Items from an Array
 Similar to Action Script
pop(); 
shift(); 

var cats = [Mr.Whiskers, Ugly Cat, Snowball]; 
var catDeath = cats.pop();    

the value catDeath is Snowball
and cats now has [Mr.Whiskers, Ugly Cat]

Adding and Deleting with splice()
this adds and deletes item from the interior positions of the array
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];


cats.splice (1,2);  
The 1 is the index number
The 2 is the number of positions to delete
var cats = [Mr.Whiskers, Ugly Cat, Snowball];
Only Mr.Whiskers and Prof.Muffintop remains! 

var catsAlive = cats.splice (1,0, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");  
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];  
0 indicate you are deleting 0 items
3 new cats are born, now catsAlive has 5 cats! 
The Circle of Life, it moves us all.


Replacing Items with splice();
add and delete at the same time
var cats = [Mr.Whiskers, Ugly Cat, Snowball, Prof.Muffintop];

var catsAlive = cats.splice (1,2, 'Snowball Jr., Son of Ugly Cat', "Lil' Kitty Winkins");
var cats = [Mr.Whiskers, Snowball Jr., Son of Ugly Cat, Lil' Kitty Winkins, Prof. Muffintop];  
 
---------------------------------------------------------------------------------------------------------------------------

TUTORIALS

document.write(' <p>'); 
you can write html into the page


prompt('Tell me your name or I'll doing a flying kick to your face');  
this produces a dialogue box similar to alert, but you retrieve an answer






Wednesday, April 6, 2011

Homeworkios

1. Get the book
2. The topic for final project website (an informational site)
the content needs to lend itself to being chopped up into subtopics
needs to have pictures because it will have image galleries
3. turn in a half page description of your site: that addresses the kinds of content, how you might organize the information, and the image gallery
4. read the chapter and take notes on the reading (you can use these notes on tests)
5. Do the following tutorials from Chapter 2. For each tutorial, create a separate file with a
filename as indicated below.
NOTE: Please type out the code yourself. You need the practice! DO NOT simply cut and
paste the code from the download site into your files. You will learn nothing, not understand
the concepts, and will probably fail the class. DO THE WORK, and you’ll learn it!
o Using Variables to Create Messages – pg 53 (name your file:
yourlastname_messages.html)
o Asking for Information – pg 54 (name your file: yourlastname_information.html)
o Writing to a Web Page Using Arrays – pg 67 (name your file:
yourlastname_arrays.html)

Lab/ Class Work

 Cybercorns!!!!  
Directions for Starting a Car.

Walk to the driver's side of the car.
Get the key into our hand.
Locate the keyhole on the driver-side door.
Placing the end that is not ridged between your thumb and index finger, insert the ridged part into the key hole on the driver-side door.
Turn the key clock-wise until you hear clicking/unlocking sound.
Remove the key.
Lift the door handle and open the driver-side door.
Swing the door all the open so that you can enter the car with your body.
Sit in the seat, to do this you place your butt into the seat.


What we learn from this exercise:
Before we start writing code we should start with psuedo-code and write in plain English the steps we plan to take.

-------------------------------

Getting Started:
Create a new "site" in Dreamweaver (specify the folder where all the files will be).
Into the head:
<script type="text/javascript">
alert("hello world");
</script>

F12 (runs the page) 
alert - is just giving you one of those alert boxes


It makes a difference where you put the code. Generally we put the code into the head of the page.

Script in the head =
If you add some text to the body and then view the page, the alert will pop up before you are able to see whatever you typed into the body.


Script in the body =
So if you put the alert into the body after the paragraph text in the body, you will see the body text before the alert.


Commenting:   
There are multiple ways in javascript to comment-out code.

//single line
/* multi-line comment */

In the body
      document.write("hello world, but this time it's better.");

--------------------------------------------------------------
Grammar (aka syntax) of JS

Statements = usually represents a single step in a program
statements end with a semicolon;

"Commands" (commands is not an official title, just something we made up for this class) = functions or methods, they use parenthesis  ( blah do something blah )

Variable = container for data, a placeholder
 for example if we have the variable called name : we all have a name but since that name is different that variable called name has a different value.

= this is not an equal sign, it is the assignment operator meaning: set to the value of
equals is a completely different thing

2 Steps to create a variable:
    - declare the variable    var name;
    -  assign it a value         name =  "J-cubed";   

               -or-   var name = "J3";

EXAMPLE:
<body>
    <h1>I am thirsty.</h1>
  
    <script type="text/javascript">

    //alert("hello world");
  
    var message;  //declared a variable
    message =  "hello world, but this time it's better."  //assign it a value
  
  
      document.write(message);   //write the message out to the page
  
</script>
</body>


Naming variables:
In JS it can only begin with a letter, $, or and _
var name, var $name, var _name
They can contain letters, numbers, $, _, camelCase  BUT NO SPACES
The variables are case sensitive

Variable Data Types
every variable you make has a specific data type, it tells you what kinda stuff is in that container
Also it informs you what you can do with it.
For example if you have a number you know you can do math on it, as opposed to a string.

We will be dealing 3 common data types:
Number- 5, 5.3, -5.3
String - any series of alphanumeric characters that are enclosed in quotes (you can use single or double quotes)
Boolean - true or false

var myVar ="5" this is a string
var myVar=5 this is a number
var hasCrap=true

---------------
Javascript is like html in that it doesn't care about white space.
But you can't put a carriage return in the middle of a string





Class Notes

Javacript

Client-Side Language
(as opposed to Server-side)

All processed by the client, they reside on the browser.
Javascript only needs a browser to run, in other words the information doesn't have to go into the cloud and do what it needs to on a server.


CSS
p { color: red; font-size: 1.5em;}

p = selector
color = declaration
1.5, red = values
font-size = property
{ blah blah } = declaration block

jquery is all based on CSS

Every programming language has a syntax ( the rules/grammar that applies to that particular language).
A computer program is a series of steps performed in a designated order.

Tuesday, October 5, 2010

Week 1

 Class Website
homework assignments, reading, syllabus

http://www.divestudio.org/aii/

Required Text(s): Javascript: The Missing Manual by David Sawyer McFarland (O’Reilly
Media) 2008. ISBN 978-0-596-51589-8.


Learning programming takes:
o Patience
o Repetition
o Faith in yourself
o Careful reading of instructions
o Breathing deeply when things get tough
o Keeping frustration to a minimum
o Keeping up since new concepts build upon previous concepts
o Taking notes on what you don’t understand, so you can ask questions later
o A willingness to do research on your own to better understand and problem‐solve
o Add other ideas of your own:








How to post code into crappy ol' blogspot:
Replace all the < characters with <
And all the > characters with >
(Exactly the same including the semicolons).


> alt 62
< alt 60