SlideShare a Scribd company logo
Introduction to
JavaScript #4
@danielknell

Friday, 18 October 13
http://artsn.co/js-tpl

Friday, 18 October 13
Recap
var data = [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9]]
;
for (var x = 0; x < data.length; ++x) {
for (var y = 0; y < data[x].length; ++y) {
console.log(data[x][y]);
}
}

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.id;
el.className;

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.setAttribute('lang', 'en');
el.getAttribute('lang');
// <div id="output" lang="en"></div>
el.setAttribute('data-foo', 'foo');
el.getAttribute('data-foo');
// <div id="output" data-foo="foo"></div>

Friday, 18 October 13
Recap
var el = document.getElementById('output');
el.addEventListener('click', callback, false); // not IE
< 9
el.attachEvent('onclick', callback); // IE < 9

Friday, 18 October 13
Recap
var el = document.getElementById('output');
function callback(e) {
alert('hello world');
e.preventDefault();
e.stopPropagation();
}
el.addEventListener('click', callback, false);

Friday, 18 October 13
Recap
Math.round(0.5); // 1
Math.floor(0.9); // 0
Math.ceil(0.1); // 1
Math.abs(-1); // 1
Math.sqrt(9); // 3
Math.sin(1);
Math.cos(1);
Math.tan(1);
Math.asin(1);
Math.acos(1);
Math.atan(1);

//
//
//
//
//
//

0.8414709848078965
0.5403023058681398
1.5574077246549023
1.5707963267948966
0
0.7853981633974483

Math.min(1, 5); // 1
Math.max(1, 5); // 5
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045

Friday, 18 October 13
Slide Puzzle

Friday, 18 October 13
Slide Puzzle
child.addEventListener('click', function(e) {
var x = this.getAttribute('data-x')
, y = this.getAttribute('data-y')
, ok = false
;
if (x == pos.x && Math.abs(y - pos.y) == 1) { ok = true; }
if (y == pos.y && Math.abs(x - pos.x) == 1) { ok = true; }
if (ok) {
this.style.left = (pos.x * pieceWidth) + 'px';
this.style.top = (pos.y * pieceHeight) + 'px';
this.setAttribute('data-x', pos.x);
this.setAttribute('data-y', pos.y);
pos.x = x;
pos.y = y;
}
});

Friday, 18 October 13
Recursion

Friday, 18 October 13
Recursion
function counter(count) {
if (count === undefined) {
count = 1;
}
console.log(count);
if (count < 10) {
counter(count + 1);
}
}
counter();

Friday, 18 October 13
Recursion

Friday, 18 October 13
Recursion

}

Friday, 18 October 13

function fib(n) {
if (n < 2) {
return n;
}
return fib(n - 1) - fib(n - 2);
Recursion

Friday, 18 October 13
Scope

Friday, 18 October 13
Scope
function greeter(greeting) {
var count = 0
;
function greet(name) {
count++;
}
}

console.log(greeting + ' ' + name + '! (#' + count + ')');

return greet;

hi = greeter('Hi');
hi('Bob'); // Hi Bob (#1)
hi('Fred'); // Hi Fred (#2)
count // undefined

Friday, 18 October 13
Classes

Friday, 18 October 13
Classes
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.name // fido
cat.name // puss

Friday, 18 October 13
Classes
function Animal(name, sound) {
this.name = name;
this.sound = sound;
}
Animal.prototype.greet = function() {
console.log(this.sound);
}
dog = new Animal('fido', 'woof!');
cat = new Animal('puss', 'meow!');
dog.greet();
cat.greet();

Friday, 18 October 13
Classes
function Dog(name) {
this.name = name;
this.sound = 'woof!';
}
Dog.prototype = new Animal(null, null);
dog = new Dog('fido');

Friday, 18 October 13
Classes
function Dog(name) {
Animal.call(this, name, 'woof!');
}
Dog.prototype = new Animal(null, null);

Friday, 18 October 13
Slide Puzzle

Friday, 18 October 13
Thats All Folks
email: contact@danielknell.co.uk
twitter: @danielknell
website: http://danielknell.co.uk/

Friday, 18 October 13

More Related Content

PDF
Javascript ES6 generators
PDF
20151224-games
PDF
2016 gunma.web games-and-asm.js
PDF
ES6 generators
PDF
Mozilla とブラウザゲーム
PDF
An Introduction to JavaScript: Week 5
PDF
Metarhia KievJS 22-Feb-2018
PDF
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
Javascript ES6 generators
20151224-games
2016 gunma.web games-and-asm.js
ES6 generators
Mozilla とブラウザゲーム
An Introduction to JavaScript: Week 5
Metarhia KievJS 22-Feb-2018
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...

What's hot (20)

DOCX
Save game function
TXT
Problemas de Arreglos en c++
TXT
Program to sort array using insertion sort
PDF
A tour of Python
PPTX
Intro to Cuda
PDF
Bytes in the Machine: Inside the CPython interpreter
PPTX
API Python Chess: Distribution of Chess Wins based on random moves
PPTX
Fixing Web Data in Production
PDF
Writing a compiler in go
PDF
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
PDF
Docopt
PDF
Codigos
DOCX
Opp compile
PDF
Меняем javascript с помощью javascript
PDF
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
PDF
Diving into byte code optimization in python
PDF
D-Talk: What's awesome about Ruby 2.x and Rails 4
PDF
ng-conf 2017: Angular Mischief Maker Slides
Save game function
Problemas de Arreglos en c++
Program to sort array using insertion sort
A tour of Python
Intro to Cuda
Bytes in the Machine: Inside the CPython interpreter
API Python Chess: Distribution of Chess Wins based on random moves
Fixing Web Data in Production
Writing a compiler in go
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Docopt
Codigos
Opp compile
Меняем javascript с помощью javascript
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Diving into byte code optimization in python
D-Talk: What's awesome about Ruby 2.x and Rails 4
ng-conf 2017: Angular Mischief Maker Slides
Ad

Viewers also liked (20)

PPT
JAVA SCRIPT
PDF
Introduction to JavaScript: Week Two
PPTX
The big bang theory - UNIT 2
PPTX
The big bang theory of social recruiting
PPTX
8. java script
PDF
An Introduction to JavaScript: Week 3
PDF
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
PPTX
Introduction to Java Script
PPT
Java Script
PDF
Unchallengeable miracle of Holy Quran
PDF
An Introduction to JavaScript: Week One
PPTX
Big Bang Theory
PPT
Java script -23jan2015
PPT
Chapter 1 - How the world begin
PPTX
Big Bang Theorychandler
PPTX
Qur’an and its sciences
PDF
Large-Scale JavaScript Development
PPT
The Quran and Computational Linguistics
PPT
Java script Learn Easy
PPTX
Evolution of universe
JAVA SCRIPT
Introduction to JavaScript: Week Two
The big bang theory - UNIT 2
The big bang theory of social recruiting
8. java script
An Introduction to JavaScript: Week 3
The Big Bang Theory: Nine Steps To Building Your Meetup Empire
Introduction to Java Script
Java Script
Unchallengeable miracle of Holy Quran
An Introduction to JavaScript: Week One
Big Bang Theory
Java script -23jan2015
Chapter 1 - How the world begin
Big Bang Theorychandler
Qur’an and its sciences
Large-Scale JavaScript Development
The Quran and Computational Linguistics
Java script Learn Easy
Evolution of universe
Ad

Similar to An Introduction to JavaScript: Week 4 (20)

PDF
Test driven game development silly, stupid or inspired?
PDF
Securing Client Side Data
PDF
Elm: give it a try
PDF
Node.js - Demnächst auf einem Server in Ihrer Nähe
PDF
What they don't tell you about JavaScript
PDF
Clojure night
PPTX
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
PPTX
Academy PRO: ES2015
PDF
ES2015 New Features
PDF
はじめてのGroovy
PDF
The Future of JavaScript (SXSW '07)
PPTX
Groovy
PDF
2024 PHPCon - Symfony background processing
PDF
Nik Graf - Get started with Reason and ReasonReact
PDF
verilog HDL introduction - beginners guide
KEY
JavaScript @ CTK
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
KEY
Blocks+gcd入門
PDF
Workshop 10: ECMAScript 6
DOC
Jsphp 110312161301-phpapp02
Test driven game development silly, stupid or inspired?
Securing Client Side Data
Elm: give it a try
Node.js - Demnächst auf einem Server in Ihrer Nähe
What they don't tell you about JavaScript
Clojure night
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
Academy PRO: ES2015
ES2015 New Features
はじめてのGroovy
The Future of JavaScript (SXSW '07)
Groovy
2024 PHPCon - Symfony background processing
Nik Graf - Get started with Reason and ReasonReact
verilog HDL introduction - beginners guide
JavaScript @ CTK
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
Blocks+gcd入門
Workshop 10: ECMAScript 6
Jsphp 110312161301-phpapp02

More from Event Handler (8)

PDF
Selling UCD: Getting buy-in and measuring the value of UX
PDF
Best Practice for UX Deliverables - 2014
PPT
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
PPTX
Tumours and Tree Trunks - GeekyScience: Evolution
PDF
Best Practice for UX Deliverables
PPSX
The Missing Ingredient
PPTX
Productivity quickly
PPTX
Anna pickard geekyfinal
Selling UCD: Getting buy-in and measuring the value of UX
Best Practice for UX Deliverables - 2014
From Chuck D to Chuck D: Evolution, Synthetic Biology and the History of Hip Hop
Tumours and Tree Trunks - GeekyScience: Evolution
Best Practice for UX Deliverables
The Missing Ingredient
Productivity quickly
Anna pickard geekyfinal

Recently uploaded (20)

PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
PPTX
Business Ethics - An introduction and its overview.pptx
PDF
DOC-20250806-WA0002._20250806_112011_0000.pdf
PPTX
HR Introduction Slide (1).pptx on hr intro
PDF
20250805_A. Stotz All Weather Strategy - Performance review July 2025.pdf
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PDF
MSPs in 10 Words - Created by US MSP Network
PDF
How to Get Funding for Your Trucking Business
PDF
Power and position in leadershipDOC-20250808-WA0011..pdf
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PDF
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
PDF
Business model innovation report 2022.pdf
PDF
A Brief Introduction About Julia Allison
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
Traveri Digital Marketing Seminar 2025 by Corey and Jessica Perlman
PPTX
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
PDF
Nidhal Samdaie CV - International Business Consultant
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
Business Ethics - An introduction and its overview.pptx
DOC-20250806-WA0002._20250806_112011_0000.pdf
HR Introduction Slide (1).pptx on hr intro
20250805_A. Stotz All Weather Strategy - Performance review July 2025.pdf
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Ôn tập tiếng anh trong kinh doanh nâng cao
MSPs in 10 Words - Created by US MSP Network
How to Get Funding for Your Trucking Business
Power and position in leadershipDOC-20250808-WA0011..pdf
340036916-American-Literature-Literary-Period-Overview.ppt
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
Business model innovation report 2022.pdf
A Brief Introduction About Julia Allison
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
Roadmap Map-digital Banking feature MB,IB,AB
Traveri Digital Marketing Seminar 2025 by Corey and Jessica Perlman
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
Nidhal Samdaie CV - International Business Consultant

An Introduction to JavaScript: Week 4