javascript from scratch: playing with data

27
JavaScript From Scratch “Playing With Data”

Upload: michael-girouard

Post on 17-Jan-2015

1.228 views

Category:

Technology


1 download

DESCRIPTION

The second of an eight part series covering the basics of the JavaScript language.

TRANSCRIPT

Page 1: JavaScript From Scratch: Playing With Data

JavaScript From Scratch“Playing With Data”

Page 2: JavaScript From Scratch: Playing With Data

Agenda

• Strings

• Numbers

• Booleans

• Arrays

• Objects

Page 3: JavaScript From Scratch: Playing With Data

Strings

• Any data surrounded by quotes

• ‘Single quotes’

• “Double quotes”

• Usually things with non-numeric characters

Page 4: JavaScript From Scratch: Playing With Data

Strings

• Can be concatenated via the + operator

var name = ‘Mike ’ + ‘Girouard’;var sentence = name + ‘ is awesome!’;

Page 5: JavaScript From Scratch: Playing With Data

var element = ‘p’;var content = ‘Hello, world!’;var tag = ‘<’ + element + ‘>’;

tag = tag + content;tag = tag + ‘</’ + element + ‘>’;

Page 6: JavaScript From Scratch: Playing With Data

var element = ‘p’;var content = ‘Hello, world!’;var tag = ‘<’ + element + ‘>’;

tag += content;tag += ‘</’ + element + ‘>’;

Page 7: JavaScript From Scratch: Playing With Data

Numbers

• Any kind of number

• 1, 2, 4, 100, 79812 (Integers)

• 1.2, 7.9, 1.2489, 1.0 (Floats)

Page 8: JavaScript From Scratch: Playing With Data

var product = ‘Widget’;var price = 1.99;var quantity = 4;var taxRate = .08375;

var subTotal = price * quantity;var taxAmt = subTotal * taxRate;var total = subTotal + taxAmt;

Page 9: JavaScript From Scratch: Playing With Data

Booleans

• True/False values

var canDrink = true;var canParkInBrooklyn = false;

Page 10: JavaScript From Scratch: Playing With Data

var isOver21 = (2008 - 1983) > 21;var isEmpty = (input.value == ‘’);var isIEorOpera = (‘all’ in document);

Page 11: JavaScript From Scratch: Playing With Data

if (isOver21) { alert(‘99 beers on the wall...’);}

Page 12: JavaScript From Scratch: Playing With Data

if (isEmpty) { alert(‘You missed something...’); input.focus();}

Page 13: JavaScript From Scratch: Playing With Data

if (isIEorOpera) { var url = ‘http://getfirefox.com/’; window.location = url;}

Page 14: JavaScript From Scratch: Playing With Data

var validLogin = (user == ‘foo’ && pass == ‘bar’);

if (!validLogin) { window.location = ‘bad-login.php’;}

Page 15: JavaScript From Scratch: Playing With Data

Arrays

• One variable, multiple values

• Use numbers to identify values

• Think about an <ol> in XHTML

• ... that starts with 0

Page 16: JavaScript From Scratch: Playing With Data

var colors = [‘red’, ‘green’, ‘blue’];

alert(colors[0]);alert(colors[1]);alert(colors[2]);

Page 17: JavaScript From Scratch: Playing With Data

var sentence = ‘There are ’ + colors.length + ‘ colors total’;

colors.push(‘chartreuse’);

sentence = ‘Now there are ’ + colors.length;

Page 18: JavaScript From Scratch: Playing With Data

Objects

• Like arrays: one variable, multiple values

• Instead of numbers, objects use strings

Page 19: JavaScript From Scratch: Playing With Data

var birthdays = { ‘Mike’ : ‘01-14-83’, ‘John’ : ‘06-23-74’, ‘Amy’ : null};

alert(birthdays.Mike);alert(birthdays.John);alert(birthdays.Amy);

Page 20: JavaScript From Scratch: Playing With Data

Review

• Strings:

• Begin and end with quotes

• Quotes can be ‘single’ or “double”

• Concatenated with a + operator

• Appended with a += operator

Page 21: JavaScript From Scratch: Playing With Data

Review

• Numbers:

• Any kind of numeric data

• Quotes are not necessary

Page 22: JavaScript From Scratch: Playing With Data

Review

• Booleans:

• true or false values

• Form the basis of all logic in programming

• Can be negated with a ! operator

Page 23: JavaScript From Scratch: Playing With Data

Review

• Arrays:

• One variable, multiple values

• Indexed by numbers

• ... beginning with 0

• New items can be added with push()

Page 24: JavaScript From Scratch: Playing With Data

Review

• Objects:

• One variable, multiple values

• Indexed by strings

Page 25: JavaScript From Scratch: Playing With Data

Homework

• Build a micro-library for HTML generation

• Functions should accept arguments for each attribute

• Functions should return strings as a result

Page 26: JavaScript From Scratch: Playing With Data

Homework

• Required Functions:

• a (href, title, content)

• img (src, alt)

• p, h1 ... h6 (content)

Page 27: JavaScript From Scratch: Playing With Data

Homework

• Required Functions:

• ol, ul (listItems)

• dl (dictionary)