Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ Edit this document to include your answers after each question. Make sure to lea

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

.map return a new array

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

The difference between a function and a method is that a method belongs to an object.

3. What is closure?

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

window: whenever this is called in the global scope this will be, the window/global object

implicit: Whenever a object calls a method 'this' is the calling object.

new: whenever the new keyword is used 'this' is the instance of object being created

explicit: Whenever this is set using the call or apply method


5. Why do we need super() in an extended class?
super() tells the parent object to be aware of the childs parameters being passed in.

super passes the

## Project Set up

Expand Down
61 changes: 58 additions & 3 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.

// function CuboidMaker(params) {
// this.length = params.length;
// this.width = params.width;
// this.height = params.height;
// }

// CuboidMaker.prototype.volume = function() {
// return this.length * this.width * this.height;
// };

// CuboidMaker.prototype.surfaceArea = function() {
// return 2 * (this.length * this.width+ this.length * this.height + this.width * this.height);
// };

class CuboidMaker {
constructor(params) {
this.length = params.length;
this.width = params.width;
this.height = params.height;
}

volume() {
return this.length * this.width * this.height;
}

surfaceArea() {
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
}
}

const cuboid2 = new CuboidMaker({
height: 5,
width: 5,
length: 4,
});
// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
console.log(cuboid2.volume()); // 100
console.log(cuboid2.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(params) {
super(params);
this.sides = params.sides;
}

volume() {
return Math.pow(this.sides, 3);
}

surfaceArea() {
return 6 * Math.pow(this.sides, 3);
}
}

const cube = new CubeMaker({ sides: 4 });

// 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(cube.volume());
console.log(cube.surfaceArea());
32 changes: 22 additions & 10 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ==== 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.
Expand All @@ -7,26 +7,38 @@
* In the body of the function return the callback with the two parameters that you created
*/

function consume(a, b, cb) {
console.log(cb(a, b));
}

/* 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!"
*/

function add(a, b) {
return a + b;
}

/* 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!
function multiply(a, b) {
return a * b;
}

function greeting(a, b) {
return `Hello ${a} ${b}, 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!

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

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

// Explanation:

// Explanation: nestedfunction can use variables from its parent scope

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

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

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