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
40 changes: 38 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,50 @@ Edit this document to include your answers after each question. Make sure to lea

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

- ```.forEach``` is an interator function that is used for callbacks to
access items in array that comes back with each pass.

- Whereas ```.map``` is similar to ```.forEach```, but the difference is
that ```.map``` returns a new array of elements while in turn passing
each element back to the callback.

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

- Conventionally, a method is a function that is a member of an object
property, seperated by a dot notation. Whereas a function is standalone
function. Technically, a function is a method on the Global Scope of the
Window Object.

3. What is closure?

- Closure is when a function is able to remember and access its lexical
scope even when that function is executing outside its lexical scope. It
only exists when an inner function makes use of variables defined from
an outer function that has returned.

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

- Window/Global Object Binding - In the global scope or not inside of a
declared object, the value of "this" defaults to the Window/Global or
Console Object. In "strict mode", "this" on Global scope returns an error.

- Implicit Binding - When keyword "this" is inside of a declared object,
"this" will be the closest parent object, the object before the dot
notation.

- New Binding - When an object is created and returned by a constructor
function using the "new" keyword, "this" refers to the instance of that
object.

- Explicit Binding - Using the call, apply or bind methods, it tells the
function what object to use as "this".

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

- We need super() in an extended class to tell a parent’s constructor
to be concerned with the child’s attributes vis versa and abstracts away
the Object.create(this, Class) syntax.

## Project Set up

Follow these steps to set up and work on your project:
Expand Down Expand Up @@ -65,11 +101,11 @@ Your finished project must include all of the following requirements:
**Pro tip for this challenge: If something seems like it isn't working locally, copy and paste your code up to codepen and take another look at the console.**

## Task 1: Objects and Arrays
Test your knowledge of objects and arrays.
Test your knowledge of objects and arrays.
* [ ] Use the [objects-arrays.js](challenges/objects-arrays.js) link to get started. Read the instructions carefully!

## Task 2: Functions
This challenge takes a look at callbacks and closures as well as scope.
This challenge takes a look at callbacks and closures as well as scope.
* [ ] Use the [functions.js](challenges/functions.js) link to get started. Read the instructions carefully!

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

class CuboidMaker {
constructor(specs) {
this.length = specs.length;
this.width = specs.width;
this.height = specs.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 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(specs) {
super(specs);
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea() {
return 6 * this.length * this.length;
}
}

const cube = new CubeMaker({
length: 3,
width: 3,
height: 3
});

// 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()); // 27
console.log(cube.surfaceArea()); // 54
23 changes: 14 additions & 9 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
// ==== 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
* The last parameter accepts a callback
* In the body of the function return the callback with the two parameters that you created
*/

const consume = (param1, param2, cb) => cb(param1, param2);

/* 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 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 = (num1, num2) => console.log(num1 + num2);
const multiply = (num1, num2) => console.log(num1 * num2);
const greeting = (firstName, lastName) =>
console.log(`Hello ${firstName} ${lastName}, 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 ====
// ==== Closures ====

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

// Explanation:
// Explanation: nestedFunction() can access the variable 'internal' because of closure via lexical scoping.


const external = "I'm outside the function";
Expand All @@ -39,4 +44,4 @@ function myFunction() {
};
nestedFunction();
}
myFunction();
myFunction();
2 changes: 1 addition & 1 deletion challenges/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<body>
<h1>Sprint Challenge - Check your work in the console!</h1>
</body>
</html>
</html>
Loading