Lesson 158, 160 and 161 – Complete 2020 Web Development Bootcamp

Lesson 158 – Higher Order Functions and Passing Functions as Arguments

The addEventListener() function takes two inputs.

addEventListener(input1, input2);

The first input is the event that is being listened for – “click”.

addEventListener("click", input2);

The second input is what the code will do once the event being listened for has happened.

addEventListener("click", respondToClick);

function respondToClick() {
  console.log("I got clicked!");
}

Higher order functions are functions that can take other functions as input.

The challenge in this lesson is to create a calculator function that takes two number inputs and an operator input so that it can add, subtract, multiply and divide the two number inputs.

This is my solution:

function add(num1, num2) {
    return num1 + num2;
}

function subtract(num1, num2) {
    return num1 - num2;
}

function multiply(num1, num2) {
    return num1 * num2;
}

function divide(num1, num2) {
    return num1 / num2;
}

function calculator(num1, num2, operator) {
    return operator(num1, num2);
}

calculator(24, 5, divide);

Lesson 160 – How to Play Sounds on a Website

This is the code you use to create an audio object –

var audio = new Audio("sounds/tom-1.mp3");
audio.play();

Lesson 161 – A Deeper Understanding of Javascript Objects

The challenge is to create a new “housekeeper1” object. This is my solution –

var housekeeper1 = {
    age: "30",
    name: "Susan",
    experience: "5 years",
    previousEmployment: "Hotel California"
};

housekeeper1;

You can access the properties within an Object by using the “.” notation. For example –

console.log(housekeeper1.name);

will console log “Susan”.

To create a constructor function, the first letter of the first word as well as all remaining words of the variable name of the constructor function have to be capitalized. So, the name of the “Bell Boy” constructor function would be –

var BellBoy () {

}

A constructor function takes a number of inputs. The inputs are the properties which you will provide when you create new objects using the constructor function.

This is an example of a constructor function –

var BellBoy (name, age, hasWorkPermit, languages) {
  this.name = name;
  this.age = age;
  this.hasWorkPermit = hasWorkPermit;
  this.languages = languages;
}

This is an example of the code you would use when using the “BellBoy construction function to create a new object –

var bellBoy1 = new BellBoy("Timmy", 19, true, ["French", "English"]);

The second challenge in this lesson is to create a new “House Keeper” constructor function and use it to create a new “houseKeeper1” object.

This is my solution –

function HouseKeeper (name, age, experience, previousEmployment) {
    this.name = name;
    this.age = age;
    this.experience = experience;
    this.previousEmployment = previousEmployment;

}

var housekeeper1 = new HouseKeeper("Susan", 30, "5 years", "Hotel California");

housekeeper1;