CS1116/CS5018

Web Development 2

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

(Acknowledgment: This way of introducing JavaScript is inspired by the methods of Seb Lee-Delisle.)

Server-side programs

The response to the client is the output from the program

Client-side programs

The response to the client is the program itself

A client-side JavaScript program

Check your understanding

JavaScript

HTML canvas

The beginnings of particles.js

let canvas;
let context;
let width;
let height;
    
document.addEventListener('DOMContentLoaded', init, false);

function init() {
    canvas = document.querySelector('canvas');
    context = canvas.getContext('2d');
    width = canvas.width;
    height = canvas.height;
}

Variables

JavaScript variables should be explicitly declared (using let or const)

PythonJavaScript
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked





print(total_pay)

# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked

print(total_pay)
let hourly_pay;
let hours_worked;
let total_pay;

hourly_pay = 9.5;
hours_worked = 35;
total_pay = hourly_pay * hours_worked;

console.log(total_pay);

// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;

console.log(total_pay);

Variables, again

But JavaScript does allow you to combine variable declaration with initialization

PythonJavaScript
hourly_pay = 9.5
hours_worked = 35
total_pay = hourly_pay * hours_worked

print(total_pay)

# Hurray! A pay rise:
hourly_pay = 10.5
total_pay = hourly_pay * hours_worked

print(total_pay)
let hourly_pay = 9.5;
let hours_worked = 35;
let total_pay = hourly_pay * hours_worked;

console.log(total_pay);

// Hurray! A pay rise:
hourly_pay = 10.5;
total_pay = hourly_pay * hours_worked;

console.log(total_pay);

Comments

Function definitions

PythonJavaScript
def print_1_to_n(n):
    for i in range(1, n+1):
        print i

        
        
print_1_to_n(10)
function print_1_to_n(n) {
    for (let i = 1; i <= n; i += 1) {
        console.log(i);
    }
}

print_1_to_n(10);

NB: Python uses indentation to denote blocks of code; JavaScript uses curly braces

Drawing a coloured rectangle

Clearing a rectangle

Calling a function at a fixed interval

particles.js

let canvas;
let context;
let width;
let height;
let x = 250;
let y = 150;
let size = 10;
let xChange = getRandomNumber(-10, 10);
let yChange = getRandomNumber(-10, 10);
    
document.addEventListener('DOMContentLoaded', init, false);

function init() {
    canvas = document.querySelector('canvas');
    context = canvas.getContext('2d');
    width = canvas.width;
    height = canvas.height;        
    window.setInterval(draw, 33);
}

function draw() {
    context.clearRect(0, 0, width, height);
    context.fillStyle = 'yellow';
    context.fillRect(x, y, size, size);
    x = x + xChange;
    y = y + yChange;
}

function getRandomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}