Slide notes for Beginners Programming
- Title slide: Hello and welcome
- Table of contents: This is what we will cover in this workshop
- Intro
- Code is just another language: When we want to talk to humans and give them instructions, we use a human language, like English, Chinese or Urdu. When we want to talk to computers and give them instructions, we use code. JavaScript in particular is code that talks to a web browser.
- JavaScript is interpreted: By the JavaScript engine inside the web browser. JavaScript, and other languages like HTML and CSS, are interpreted to output web pages that a computer user can use.
- How the Web works:
- When a web user types a web address into their address bar or follows a link, their browser requests the web page from the server it is stored on.
- If the request is successful, the server sends the web page files (code, images, etc.) back to the browser
- The browser turns all these files into a web page and shows it to the user.
- Server-side vs client-side: Server-side code is run on the server, and the result is then sent to the client and displayed. Think of the code that finds the results when you do an Amazon search. Client-side code on the other hand is sent to the client as is, then interpreted by the client and displayed. We are only covering client-side code in this course.
- HTML: HTML is the language you use to structure your page content and give it meaning. HTML is composed of elements: tags that wrap around content.
- CSS: CSS is the language you use specify what you want your content to look like. It is made up of rules that select element to style, and change their style properties to new values.
- JavaScript: The JavaScript language is comprised of objects that you manipulate to create exciting dynamic functionality on web pages. Programming for the Web.
- Browser developer tools: Every browser has a set of developer tools that can be used to solve problems with web site code (fix bugs). These include a console that can be used to directly test the page's JavaScript. Developer tools are accessed using Ctrl + Shift + I (Cmd + Opt + I on Mac OSX, F12 on Internet Explorer)
- Variables
- Variables: You create a variable using the
var
keyword, followed by the variable name you choose. You assign a variable using the assignment operator, for example myVariable = "Chris"
. You can assign the variable different values after it has been created.
- Common variable data types: An integer is a whole number value, and is assigned without quote. A string is a series of characters, and must be surrounded in quotes. A boolean is a
true
/false
value. Arrays and other objects return a data type of object
.
- Syntax rules: There are various syntax rules you need to remember about JavaScript. The most important ones are listed here.
- Operators: These slides list the most common JS operators. Most of these are obvious.
+
is used to add numbers together and join strings together. Modulo returns the remainder after one number is divided by another, and is mainly useful for detecting whether a number is even or odd. For example, 4 % 2
is 0, as 2 divides exactly into 4. 5 % 2
is 1, as 2 divides into 5 twice, with 1 left over.
- Operators: These operators are mostly used to compare values (comparison operators). We've already seen assignment, and we'll see a lot more of the others later in the course. For example,
<
tests whether one value is less than another — 3 < 5
returns true
, because 3 is less than 5.
- Operator precedence is the order in which operators are executed in a sum. Division/multiplication are executed first, then addition/subtraction. This is why
8 + 4 / 3 * 4
perhaps doesn't return the result you thought it might. The divide is run first, then the multiply, then finally the addition. You can force the order using brackets, for example (8 + 4) / (3 * 4)
- Conditions and loops
- Conditional code allows us to test a condition (for example
5 < 3
.) If the condition returns true
then one block of code will be run. If it returns false
, something else will happen. Let's look at the code structure.
- if ... else pseudocode: The basic
if ... else
code structure looks like this.
- if ... else example: and here is a real code example. Here we prompt the user to enter a town/city name. If they enter "Manchester" then they'll see the first alert message. If they enter anything else, they'll see the second alert.
- if ... else if ... else example:
else if() {}
is used to chain a further condition check onto the code block.
- logical OR and AND: As you can see in the code examples, these operators can be used to create a conditional check where either or both conditions need to return
true
before the overall condition will return true
.
- Nesting if ... else: This is just fine.
- Loops: These code structures allow you to run the same code several times, updating part of the code each time to iterate through a set of data for example.
- For loop pseudocode: The for loop structure looks like this.
- For loop features
i
— variable
- starting value — what
i
begins at
- exit-condition — what value
i
has to be for the loop to keep running
- iterator — how
i
changes each time the loop is run
- Run code — the code run each time the loop runs
- For loop example: Here is a real for loop example.
- Document Object Model
- The browser converts HTML to a DOM tree: The Document node is the top node in the tree. Then all the elements in your page are converted into nodes, which form a hierarchy based on how they are nested. The html node is top, then the head and body, then the elements inside those.
- JavaScript and CSS accessing the DOM: When you used CSS and JavaScript to affect nodes in the DOM, they walk up and down the tree until they find the element(s) they want.
- Example: JavaScript updating element contents: In this example, we grab a reference to the
<h1>
element using the document.querySelector()
function, then store it in a variable called myH1
. We then update the content inside the element by setting a new value for myH1
's innerHTML
property.
- Canvas
- Canvas is a way to script images using JavaScript. This doesn't sound very impressive to begin with, but you can do a lot with this, such as coding games inside the browser.
- Create a canvas + get a context: To manipulate a canvas, there are three steps to go through:
- Put a
<canvas>
element onto your HTML page.
- Grab a reference to that element and store it in a variable.
- Use the
getContext()
function of the canvas variable to get a 2D drawing context; store this context in a variable. This can then be manipulated to update the <canvas>
element.
- Draw squares: it is easy to draw squares in canvas —
fillRect
and strokeRect
allow you to draw filled and outline rectangles, respectively. Their four arguments are the x and y coordinates on the canvas, and the x and y dimensions. You cna use fillStyle
and strokeStyle
to set the colours the rectangles will be, and lineWidth
will set the thickness of the stroke.
- Canvas loops: It doesn't take much imagination to start combining loops with drawing on a canvas, to create pretty patterns.
- More loop ideas: Here are some more ideas for you to try in your loops
- More loop ideas: And some more! Bear in mind that there is no easy primitive to draw a circle like there is for a rectangle, so you need to start a drawing path with
beginPath()
, draw a 360 degree arc()
, and then end the drawing path with fill()
.
- Functions are blocks of code that can be defined once, and then called as many times as you like, meaning much more efficient code. We've already used a number of JavaScript's built-in functions, but here we'll look at how to define our own.
- Sample function: In this sample function, we see one way of defining a function — using the
function
keyword, followed by the name you want your function to have, followed by brackets(()
), followed by curly braces ({}
). Inside the curly brances we put our function code. In this case we are generating a random number between 1 and 10, after which we return the result out of the function using the return
keyword. This in effect is the result you get returned when you run the function. You have to call (run) the function using its name followed by brackets.
- Function with argument: In this case the code structure is the same, except that inside the brackets we include an argument — this is a variable that we can use inside the function, depending on what result you want. In this case we can now generate a random number between 1 and any number, by specifying it as the argument when we call the function.
- Canvas function: This slide shows a simple example of a function that can generate a canvas sine wave of a certain length, at a certain position on the y axis, as specified by two arguments (separated by commas). A good example of how to encapsulte complex code so you only have to write it once.
- Events
- Events are... a feature of JavaScript that allow us to run code in response to user actions — such as clicking a button or moving the mouse — or actions run by other parts of the code — such as a database successfully being saved.
- Sample event usage (anonymous function): The simplest event example involes a button in your HTML. We grab a reference to this button, then attach an event handler property to it —
onclick
. Its value is set to an anonymouse function that wil fun when the button is clicked.
- Sample event usage (named function): This example does the same thing, but using a named function (which is the same structure as we saw earlier in the functions section.) This fnuction is rthen called on a separate line, but without the brackets you'd usaually use. This is because including the brackets would run the function straight away, but you don't want it to run until you've clicked the button. This code is more complex, but more reusable. You could potentially call this function multiple times.
- Credits