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
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ Demonstrate your understanding of this week's concepts by answering the followin
Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your project manager

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

foreach is an iterator that goes overv the existing array
map is an iterator creates a new array
2. What is the difference between a function and a method?

a function is a variable that can be called upon on its own and be used in other variables.
a method is a function whose scope is limited to the variable in which it is located
3. What is closure?

Closure is when nested functions can call on variables of the variable that they are located.
4. Describe the four rules of the 'this' keyword.

(1) when used globally it applies to the window
(2) when preceded by a dot its implicityly bound to the function in which it is located
(3) wwhen used in class functions it applies to the specific instance in which it was used
(4) if call or apply is ued than this is explicitly bound
5. Why do we need super() in an extended class?

super gives us access to the variables of the parent class
## Project Set up

Follow these steps to set up and work on your project:
Expand Down
16 changes: 16 additions & 0 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.


class CuboidMaker {
constructor (cuboid) {
this.length = cuboid.length;
this.width = cuboid.width;
this.height = cuboid.height;
}
volume() {
return this.length * this.width * this.height
}
surfaceArea() {
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height)
}
};


// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
Expand Down
25 changes: 21 additions & 4 deletions challenges/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,44 @@
* The last parameter accepts a callback
* In the body of the function return the callback with the two parameters that you created
*/
function consume(food, liquid, callback) {
return callback(food, liquid)
};



/* 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(num1, num2) {
console.log(num1 + num2);
}

function multiply(num1, num2) {
console.log(num1 * num2);
}

function greeting(fname, lname) {
return `Hello ${fname} ${lname}, 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!
consume(2,2,add); // 4
consume(10,16,multiply); // 160
consume("Mary","Poppins", greeting); // Hello Mary Poppins, nice to meet you!


// ==== Closures ====

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

// Explanation:

// Explanation:
// It can access because when JS made its first pass it was called in the nested function, this added a closure for the variable so it could be called outside.

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

Expand Down
22 changes: 19 additions & 3 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@
/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/
function CuboidMaker (cuboid) {
this.length = cuboid.length;
this.width = cuboid.width;
this.height = cuboid.height;
};



/* == Step 2: Volume Method ==
Create a method using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height

Formula for cuboid volume: length * width * height
*/
CuboidMaker.prototype.volume = function() {
return this.length * this.width * this.height
}


/* == Step 3: Surface Area Method ==
Create another method using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height.

Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height)
*/

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

/* == Step 4: Create a new object that uses CuboidMaker ==
Create a cuboid object that uses the new keyword to use our CuboidMaker constructor
Add properties and values of length: 4, width: 5, and height: 5 to cuboid.
*/
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