Programming for beginners

Mozilla and you

Programming for beginners

Brought to you by the Mozilla Developer Network

  1. Intro
  2. Variables
  3. Conditions & loops
  4. Document Object Model (DOM)
  5. <canvas>
  6. Functions
  7. Events

Intro

Code is just another language

JavaScript is interpreted

How the Web works

  1. Browser (client) requests web page
  2. Request sent to server
  3. Server sends code to browser
  4. Browser turns code into web pages/apps

Server-side vs client-side

HTML

CSS

JavaScript

Browser developer tools

Variables

Variables

Run these in the browser console

Common variable data types

Syntax rules

Operators

Operators

Operator precedence

Conditions & loops

Conditional code

if ... else pseudocode

			if(condition === true) {
			  // run expression 1
			} else {
			  // run expression 2
			}
		

if ... else example

			var myTown = prompt('Enter your home town or city');
			if(myTown === 'Manchester') {
			  alert('Is it still raining in Manchester?');
			} else {
			  alert('I don\'t know your home town very well');
			}
		

if ... else if ... else example

			if(myTown === 'Manchester') {
			  alert('Is it still raining in Manchester?');
			} else if(myTown === 'Barcelona') {
			  alert("You have beautiful architecture, and sun");
			} else {
			  alert('I don\'t know your home town very well');
			}
		

logical OR and AND

			if(myTown === "Manchester" || myTown === "Bradford") {
			  alert("It is rainy in those Northern towns.");
			}
			 
			if(myTown === "Manchester" && mySeason === "Summer") {
			  alert("Manchester must be enjoying some warmer weather.");
			}
		

Nesting if ... else

			if(condition1 === true) {
			  if(condition2 === true) {
			    // run expression 1 {
			  }
			} else {
			  // run expression 2
			}
		

Loops

For loop pseudocode

			for(i = starting value; exit-condition ; iterator) {
			  // run code
			}
		

For loop features

For loop example

			for(i = 0; i <= 20; i++) {
			  console.log(i);
			}
		

Document Object Model

The browser converts HTML to a DOM tree

JavaScript and CSS accessing the DOM

  1. JS/CSS "walk" up and down the tree
  2. Find elements they are interested in
  3. Do stuff to those elements

This is how many useful things are done on a web page

Example: JavaScript updating element contents

<h1 class="myHeading">My heading</h1>

			var myH1 = document.querySelector('.myHeading');
			myH1.innerHTML = "My updated heading";
		

Canvas

<canvas> is

  1. A scriptable HTML element
  2. Create images using JavaScript
  3. Can then animate them, etc.

Create a canvas + get a context

<canvas width="500" height="500"> </canvas>

			var myCanvas = document.querySelector('canvas');
			var myContext = myCanvas.getContext('2d');
		

Draw squares

			myContext.fillStyle = 'rgba(0,0,255,0.5)';
			myContext.fillRect(0,0,200,200);
			 
			myContext.strokeStyle = 'rgba(0,255,0,0.5)';
			myContext.lineWidth = 1;
			myContext.strokeRect(200,200,50,50);
		

Canvas loops

			myContext.fillStyle = 'rgba(0,0,255,0.15)';
			for(i = 0; i <= 50; i++) {
			  myContext.fillRect(i,i,25+i,25+i);
			}
		

More loop ideas

More loop ideas

Functions

Functions are...

Sample function

          function random10() {
            var randomNumber = Math.floor(Math.random() * 10) + 1;
            return randomNumber;
          }
           
          random10(); // call function
        

Function with argument

          function random(input) {
            var randomResult = Math.floor(Math.random() * input) + 1;
            return randomResult;
          }
           
          random(250); // call function
        

Canvas function

          function sineWave(number,y) {
            for(i=0;i<=number;i++) {
              myContext.fillStyle = 'rgba(0,0,'
               + (Math.floor(Math.random()*200)) + ',0.5)';
              myContext.fillRect((i*5),(y+(Math.sin(i/6))*50),10,10);
            }
          }
        

Events

Events are...

Sample event usage (anonymous function)

<button class="welcome">Click me!</button>

          var button = document.querySelector('.welcome');
           
          button.onclick = function() {
            alert("Ouch, that hurt!");
          }
        

Sample event usage (named function)

          var button = document.querySelector('.welcome');
           
          function clicky() {
            alert("Ouch, that hurt!");
          }
           
          button.onclick = clicky;
        

Credits

Fork me on Github