+ All Categories
Home > Documents > Introduction to JavaScript: Week Two

Introduction to JavaScript: Week Two

Date post: 06-Nov-2014
Category:
Upload: event-handler
View: 1,181 times
Download: 0 times
Share this document with a friend
Description:
 
43
Introduction to JavaScript #2 @danielknell Friday, 4 October 13
Transcript
Page 1: Introduction to JavaScript: Week Two

Introduction to JavaScript #2

@danielknell

Friday, 4 October 13

Page 2: Introduction to JavaScript: Week Two

Recap 4 * 2;4 / 2;4 + 2;4 - 2;100 % 3;99 + (9 / 9);-3;-(3+1);

Friday, 4 October 13

Page 3: Introduction to JavaScript: Week Two

Recap var x = 5;var y = 99 + (9 / 9);var z = x * y;

x++;x--;

Friday, 4 October 13

Page 4: Introduction to JavaScript: Week Two

Recap "hello";'world';"hello" + "world";"five plus two equals: " + (5 + 2);

var who = 'world';var greeting = "hello" + who;

greeting[0]

Friday, 4 October 13

Page 5: Introduction to JavaScript: Week Two

Recapvar x = null;var y;

y === undefined

Friday, 4 October 13

Page 6: Introduction to JavaScript: Week Two

Recaptrue && true;false || true;!false;

falsy:0""nullundefined

Friday, 4 October 13

Page 7: Introduction to JavaScript: Week Two

Recap1 == "1";1 != "2";2 > "1";1 < "2";2 >= "1";1 <= "2";

1 === 1;1 !== "1";

Friday, 4 October 13

Page 8: Introduction to JavaScript: Week Two

Recapvar greet = function(who) { return "hello" + who;}

function greet(who) { return "hello" + who;}

greet("world");

Friday, 4 October 13

Page 9: Introduction to JavaScript: Week Two

Recapvar a = [1, 2, "three"];

a[0]; // 1a[2]; // "three"a.length; // 3a[3]; // undefiend

Friday, 4 October 13

Page 10: Introduction to JavaScript: Week Two

Recapvar a = [1, 2, "three"];

a.push("four");a; // [1, 2, "three", "four"]

var last = a.pop();a; [1, 2, "three"]last; // "four"

var first = a.shift();a; [2, "three"]first; // 1

a.unshift("one");a; ["one", 2, "three"]

Friday, 4 October 13

Page 11: Introduction to JavaScript: Week Two

Recapvar coords = { x: 1, "y": 2 };

coords["x"]; // 1coords.y; // 2coords.z; // undefined

{ var: 1 }{ "var": 1 }

Friday, 4 October 13

Page 12: Introduction to JavaScript: Week Two

Recap if (light === 'green') { crossTheRoad();}else if (light === 'red') { stop();}else { lookConfused();}

Friday, 4 October 13

Page 13: Introduction to JavaScript: Week Two

Recap switch (light) {case 'blue':case 'green': crossTheRoad(); break;case 'red': stop(); break;default: lookConfused();}

Friday, 4 October 13

Page 14: Introduction to JavaScript: Week Two

Recap var x = 1;

// this does not workswitch (x) {case x > 1: console.log('A'); break;case x === 1: console.log('B'); break;}

Friday, 4 October 13

Page 15: Introduction to JavaScript: Week Two

Recap while (light === 'red') { wait();}crossTheRoad();

Friday, 4 October 13

Page 16: Introduction to JavaScript: Week Two

Recap do { wait();} while (light === 'red');

crossTheRoad();

Friday, 4 October 13

Page 17: Introduction to JavaScript: Week Two

Recap for (var i = 0; i < 8; i++) { potato(i);}more();

Friday, 4 October 13

Page 18: Introduction to JavaScript: Week Two

Recap while (light === 'red') { if (axeMurder === true) { break; } wait();}crossTheRoad();

Friday, 4 October 13

Page 19: Introduction to JavaScript: Week Two

Recap do { wait(); if (light === 'flashing green') { continue; }} while (light === 'red');crossTheRoad();

Friday, 4 October 13

Page 20: Introduction to JavaScript: Week Two

Recapvar person = { name: "bob", greet: function() { return "hi " + this.name + "!"; }}

person.greet();

Friday, 4 October 13

Page 21: Introduction to JavaScript: Week Two

Recapif (something) { }else { }

something();

Friday, 4 October 13

Page 22: Introduction to JavaScript: Week Two

Recapfor (var i = 1; i <= 100; ++i) { if (i % 3 === 0 && i % 5 === 0) { console.log('FizzBuzz'); } else if (i % 3 === 0) { console.log('Fizz'); } else if (i % 5 === 0) { console.log('Buzz'); } else { console.log(i); }}

Friday, 4 October 13

Page 23: Introduction to JavaScript: Week Two

http://artsn.co/js-tpl

Friday, 4 October 13

Page 24: Introduction to JavaScript: Week Two

DOM

Friday, 4 October 13

Page 25: Introduction to JavaScript: Week Two

Sorry

Friday, 4 October 13

Page 26: Introduction to JavaScript: Week Two

Really, really sorry

Friday, 4 October 13

Page 27: Introduction to JavaScript: Week Two

DOM<!DOCTYPE html><html lang="en"> <head> <title>Hello World</title> <link rel="stylesheet" href="assets/css/main.css"> </head>

<body> <div id="output"></div> <form id="reload"> <button class="submit” type="submit">Random</button> </form> <script src="assets/js/main.js"></script> </body></html>

Friday, 4 October 13

Page 28: Introduction to JavaScript: Week Two

DOM

Friday, 4 October 13

Page 29: Introduction to JavaScript: Week Two

Finding Elementsvar element = document.getElementById('puzzle');

var elements = document.getElementsByTagName('div');

var elements = document.getElementsByClassName('submit'); // not IE < 9

element.getElementById('child');

element.getElementsByTagName('span');

element.getElementsByClassName('foo');

Friday, 4 October 13

Page 30: Introduction to JavaScript: Week Two

Finding Elements#foobar

<div id="foobar"></div>

.foobar

<div class="foobar"></div>

div

<div></div>

div#foobar

<div id="foobar"></div> <p id="foobar"></p>

div.foobar

<div class="foobar"></div> <p class="foobar"></p>

div .foobar

<div id="baz"><p id="foobar"></p></div><div id="foobar"></div>

Friday, 4 October 13

Page 31: Introduction to JavaScript: Week Two

Finding Elementsvar element = document.querySelector('#reload .submit'); // not IE < 8

var elements = document.querySelectorAll('#reload .submit'); // not IE < 8

element.querySelector('#reload .submit'); // not IE < 8

element.querySelectorAll('#reload .submit'); // not IE < 8

Friday, 4 October 13

Page 32: Introduction to JavaScript: Week Two

Traversing Elements var children = element.children;

var parent = element.parentNode;

Friday, 4 October 13

Page 33: Introduction to JavaScript: Week Two

Traversing Elements

Friday, 4 October 13

Page 34: Introduction to JavaScript: Week Two

Creating Elements var parent = document.getElementById('foobar');

var element = document.createElement('div');

parent.appendChild(element);

parent.insertBefore(element, someOtherElement);

parent.removeChild(element);

element.parentNode.removeChild(element);

Friday, 4 October 13

Page 35: Introduction to JavaScript: Week Two

Changing Elements var el = document.getElementById('output');

el.textContent = 'hello world';

Friday, 4 October 13

Page 36: Introduction to JavaScript: Week Two

Fizz Buzz DOM

Friday, 4 October 13

Page 37: Introduction to JavaScript: Week Two

Fizz Buzz DOM•Create a new <ol> element

•Append in the output div

•Create a new <li> element

•Set the contents to the line from fizzbuzz

•Append it to the <ol>

•Create more <li> elements

Friday, 4 October 13

Page 38: Introduction to JavaScript: Week Two

Fizz Buzz DOMvar ol = document.createElement('ol') , el ;

document.getElementById('output').appendChild(ol);

for (var i = 1; i <= 100; ++i) { el = document.createElement('li');

if (i % 3 === 0 && i % 5 === 0) { el.textContent = 'FizzBuzz'; } else if (i % 3 === 0) { el.textContent = 'Fizz'; } else if (i % 5 === 0) { el.textContent = 'Buzz'; } else { el.textContent = i; } ol.appendChild(el);}

Friday, 4 October 13

Page 39: Introduction to JavaScript: Week Two

Element Stylevar el = document.getElementById('output');

// <div id="output" style="background-image: url('bg.jpg')"></div>

el.style.backgroundImage = "url('bg.jpg')";

el.style.fontWeight = ‘bold’;

el.style.color = ‘red’;

window.getComputedStyle(el).backgroundImage; // not IE < 9

el.currentStyle.backgroundImage; // IE < 9

Friday, 4 October 13

Page 40: Introduction to JavaScript: Week Two

“Pretty” Fizz Buzz

Friday, 4 October 13

Page 41: Introduction to JavaScript: Week Two

“Pretty” Fizz Buzz

•Loop over the list elements in a new loop

•Set all elements containing the text Fizz, Buzz, and FizzBuzz to bold

•Make all odd lines red

Friday, 4 October 13

Page 42: Introduction to JavaScript: Week Two

“Pretty” Fizz Buzzvar items = ol.children ;

for (var i = 0; i < items.length; ++i) { el = items[i];

if ( el.textContent === 'FizzBuzz' || el.textContent === 'Fizz' || el.textContent === 'Buzz' ) { el.style.fontWeight = 'bold'; }

if (i % 2 === 0) { el.style.color = 'red'; }}

Friday, 4 October 13

Page 43: Introduction to JavaScript: Week Two

Thats All Folksemail: [email protected]

twitter: @danielknell

website: http://danielknell.co.uk/

Friday, 4 October 13


Recommended