<!-- /exercises/01-js-variables.html -->
let name = "David";
function greetTeacher() {
console.log("Hi, " + name);
}
name = "not David";
<!-- What will we see in the console? -->
greetTeacher();
movie and director are created inside createCollab()
<!-- /exercises/02-js-scoping.html -->
let musicalArtist = "David Bowie";
function createCollab() {
let movie = "Labyrinth";
let director = "Jim Henson";
console.log("\"" + movie + "\" by " + director + " feat. " + musicalArtist);
}
createCollab();
<!-- This won't work. Why? -->
console.log(director);
let vs var
var is from the older js days
var quite often, so you should know how it differs from let
<!-- /exercises/03-js-scoping-let-var.html -->
function sayHelloVar() {
if (true) {
var phraseA = "Hello";
}
console.log("[ sayHelloVar() ] " + phraseA);
}
function sayHelloLet() {
if (true) {
let phraseB = "Hello";
}
console.log("[ sayHelloLet() ] " + phraseB);
}
<!-- This works! -->
sayHelloVar();
<!-- This won't work. Why? -->
sayHelloLet();
<!-- /exercises/04-js-array-add-remove.html -->
let fruits = ["Granny Smith Apple", "Honeycrisp Apple", "Fiji Apple" ];
<!-- push() adds to the end of the array -->
fruits.push("Gala Apple");
<!-- pop() removes from the end of an array -->
fruits.pop();
<!-- unshift adds to the beginning of an array -->
fruits.unshift("Jazz Apple");
<!--
splice() can add and remove at specified indices
here is an example of removing at an index
say we want to remove "Honeycrisp Apple"
first, we find its index (handy for lengthy arrays)
-->
let index = fruits.indexOf("Honeycrisp Apple");
<!-- if the array has "Honeycrisp Apple", we remove it -->
<!-- if index is -1, it doesn't exist in the array -->
if (index > -1) {
fruits.splice(index, 1);
} else {
console.log("Honeycrisp Apple does not exist in the array.");
}
<!-- /exercises/05-js-array-indexing.html -->
let fruits = ["Apple", "Fig", "Pomegranate", "Mulberry"];
<!-- returning by index (recall that we start at 0) -->
console.log("My favorite fruit is: " + fruits[2]);
<!-- looping through an array -->
<!-- arrays are objects! they have a property called length. -->
for (let i = 0; i < fruits.length; i++) {
console.log('fruits[' + i + '] is: ' + fruits[i]);
}
filter() and map()
filter() and map()
<!-- /exercises/06-js-array-functions.html -->
let fruits = [
"Granny Smith Apple",
"Honeycrisp Apple",
"Fiji Apple",
"Gala Apple",
"Strawberry",
"Blackberry",
"Raspberry",
"Yellow Nectarine",
"White Nectarine",
"Navel Orange"
];
<!-- filter -->
let onlyApples = fruits.filter(function(fruit) {
return fruit.indexOf("Apple") > -1;
});
console.log(onlyApples);
<!-- map -->
let editedFruits = fruits.map(function(fruit) {
return fruit + "s are yummy";
});
console.log(editedFruits);
this refers to this particular object
<!-- /exercises/07-js-objects.html -->
let grammyWinner = {
name : "Supernatural",
Artist : "Santana",
age : 23,
releaseDate : "June 15, 1999",
grammyWins : 9,
updateRecords : function(amt) {
<!-- What is 'this'? -->
this.grammyWins += amt;
}
};
grammyWinner.updateRecords(3);
<!-- /exercises/07-js-objects.html -->
let grammyWinner = {
name : "Supernatural",
Artist : "Santana",
age : 23,
releaseDate : "June 15, 1999",
grammyWins : 9,
updateRecords : function(amt) {
<!-- What is 'this'? -->
this.grammyWins += amt;
}
};
<!--
If you try this in the console you'll notice it won't work as
expected, you'll get grammyWinner: [object Object]
-->
console.log('grammyWinner: ' + grammyWinner);
<!-- You can use JSON.Stringify() to print the contents as a string -->
console.log(JSON.stringify(grammyWinner));
<!-- Or you can use this to get an expandable object in the dev tools -->
console.log('grammyWinner:', grammyWinner);
document, window, console
onchange, onkeydown, onmouseover, onsubmit, onresize
<!-- /exercises/08-js-events.html -->
<body>
<div id="interactive-div">
<span id="click-text">Click to change my color!</span>
</div>
</body>
<!-- /exercises/08-js-events.html -->
let elem;
window.addEventListener('load', init);
function init() {
elem = document.getElementById('interactive-div');
elem.addEventListener('click', function() {
elem.style.background = getRandomColor();
});
}
<!-- generate random hex values -->
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
<canvas>
<canvas> is just another HTML element
createCanvas(400, 400);
<canvas> we need to get its rendering context
<!-- /exercises/09-js-canvas-intro.html -->
<body>
<canvas id="my-canvas"></canvas>
</body>
<!-- /exercises/09-js-canvas-intro.html -->
let canvasElement = document.getElementById('my-canvas');
let ctx = canvasElement.getContext('2d');
<!-- /exercises/09-js-canvas-intro.html -->
canvasElem.width = 500;
canvasElem.height = 500;
function draw() {
<!-- to set a background, we create a rect and fill it -->
ctx.fillStyle = "#520086";
ctx.fillRect(0, 0, canvasElem.width, canvasElem.height);
<!-- draw a circle -->
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = "black";
ctx.fill();
ctx.lineWidth = 2;
ctx.strokeStyle = "#cfcfff";
ctx.stroke();
}
<script> tags in your HTML
import or include or require