Skip to content
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ Edit this document to include your answers after each question. Make sure to lea

1. Describe the biggest difference between `.forEach` & `.map`.

- forEach executes a callback function on each element of the array, one after the other. map returns a new array with each element having been put through the provided callback.

2. What is the difference between a function and a method?

- Methods are just functions that are stored in/applied to objects. All methods are functions, but not all functions are methods.

3. What is closure?

- Closure is about functions having access to variables assigned above their own scope. This does not work in the opposite direction.

4. Describe the four rules of the 'this' keyword.

1) By default, without any of the rules below taking effect, this binds to the global object/ the Window.
2) Implicit binding is when this is bound to something by dot notation, so it refers to whatever sits to the left of that dot.
3) Explicit binding refers to using call, apply or bind to bind this to a certain object.
4) New binding is when a constructor object is used to instantiate an object using the new keyword, thereby binding this to the new object.

5. Why do we need super() in an extended class?

- It lets the extended class inherit the properties and methods of the class which it extends.

## Project Set up

Follow these steps to set up and work on your project:
Expand Down
66 changes: 63 additions & 3 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.
class CuboidMaker {
constructor(attrs) {
this.length = attrs.length;
this.width = attrs.width;
this.height = attrs.height;
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea() {
return (
2 *
(this.length * this.width +
this.length * this.height +
this.height * this.width)
);
}
}

const cuboid = new CuboidMaker({
length: 4,
width: 5,
height: 5
});

// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130

// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area.

class CubeMaker extends CuboidMaker {
constructor(int) {
super(int);
this.length = int;
this.width = int;
this.height = int;
}
}

const cube = new CubeMaker(10);

// Inherited methods still work great!

console.log(cube.volume()); // 1000
console.log(cube.surfaceArea()); // 600

// Alternately, with new methods:

class CubeMakerTwo extends CuboidMaker {
constructor(int) {
super(int);
this.edge = int;
}
volume() {
return this.edge ** 3;
}
surfaceArea() {
return this.edge ** 2 * 6;
}
}

const cubeTwo = new CubeMakerTwo(15);

// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area.
console.log(cubeTwo.volume()); // 3375
console.log(cubeTwo.surfaceArea()); // 1350
45 changes: 25 additions & 20 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
// ==== Callbacks ====
// ==== Callbacks ====

/* Step 1: Create a higher-order function that accepts a callback
* Create a higher-order function named consume that can take 3 parameters.
* The first two parameters can accept any argument
* The last parameter accepts a callback
* In the body of the function return the callback with the two parameters that you created
*/

* Create a higher-order function named consume that can take 3 parameters.
* The first two parameters can accept any argument
* The last parameter accepts a callback
* In the body of the function return the callback with the two parameters that you created
*/

function consume(x, y, cb) {
return cb(x, y);
}

/* Step 2: Create several functions to callback with consume();
* Create a function named add that returns the sum of two numbers
* Create a function named multiply that returns the product of two numbers
* Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!"
*/
* Create a function named add that returns the sum of two numbers
* Create a function named multiply that returns the product of two numbers
* Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!"
*/

const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
const greeting = (first, last) => `Hello ${first} ${last}, nice to meet you!`;

/* Step 3: Check your work by un-commenting the following calls to consume(): */
// consume(2,2,add); // 4
// consume(10,16,multiply); // 160
// consume("Mary","Poppins", greeting); // Hello Mary Poppins, nice to meet you!
console.log(consume(2, 2, add)); // 4
console.log(consume(10, 16, multiply)); // 160
console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you!


// ==== Closures ====
// ==== Closures ====

// Explain in your own words why `nestedfunction()` can access the variable `internal`.

// Explanation:

// Explanation:
// Closure! Closure gives functions access to variables defined above their own scope, whether it's a global variable or from a previous function. It's the same reason `myFunction()` can access `external`. But, for instance, putting `console.log(internal)` at the bottom of all this would throw an error, since it doesn't go in the opposite direction.

const external = "I'm outside the function";

Expand All @@ -36,7 +41,7 @@ function myFunction() {

function nestedFunction() {
console.log(internal);
};
}
nestedFunction();
}
myFunction();
myFunction();
Loading