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

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

.map automatically returns a new array whereas .forEach does not. This means that .forEach tends to be used to mutate an existing array and .map tends to be used to create a new array from an existing one.

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

A function is any block of code defined as a function. A method is a function that is defined specifically within an object which typically somehow references that object.

3. What is closure?

A closure is what is created to allow functions to access variables that exist in the parent scope but do not exist within the functions scope. This has the effect of allowing you to access variables in a function that don't exist in the function but do exist in the lexical scope in which the function was defined. The closure itself consists of both the function and the lexical environment in which it was defined.

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

1. Global Binding. When "this" is used in the global scope it will refer to the window object. If "use strict" is enabled it will be undefined.
2. Implicit Binding. This occurs when an object method uses "this". Here "this" refers to the object on which the method was called.
3. New Binding. A binding of "this" which is created when using the "new" keyword to instantiate an object. Here "this" refers to the object that has been created by invoking the constructor function.
4. Explicit Binding. Occurs when .call, .apply, or .bind is called. Here "this" refers to the object that is passed into the method.

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

The super keyword is needed to apply properties from a parent class onto a child class when using class syntax. This functions by calling the parent constructor. It can also be used to call a function on a parent class.

## Project Set up

Follow these steps to set up and work on your project:

- [ ] Create a forked copy of this project.
- [ ] Add TL as collaborator on Github.
- [ ] Clone your OWN version of Repo (Not Lambda's by mistake!).
- [ ] Create a new Branch on the clone: git checkout -b `<firstName-lastName>`.
- [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
- [ ] You are now ready to build this project with your preferred IDE
- [ ] Implement the project on your Branch, committing changes regularly.
- [ ] Push commits: git push origin `<firstName-lastName>`.
- [x] Create a forked copy of this project.
- [x] Add TL as collaborator on Github.
- [x] Clone your OWN version of Repo (Not Lambda's by mistake!).
- [x] Create a new Branch on the clone: git checkout -b `<firstName-lastName>`.
- [x] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
- [x] You are now ready to build this project with your preferred IDE
- [x] Implement the project on your Branch, committing changes regularly.
- [x] Push commits: git push origin `<firstName-lastName>`.

Follow these steps for completing your project:

- [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo).
- [x] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo).
- [ ] Add your team lead as a Reviewer on the Pull-request
- [ ] TL then will count the HW as done by merging the branch back into master.

Expand All @@ -66,19 +79,19 @@ Your finished project must include all of the following requirements:

## Task 1: 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!
* [x] 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.
* [ ] Use the [functions.js](challenges/functions.js) link to get started. Read the instructions carefully!
* [x] Use the [functions.js](challenges/functions.js) link to get started. Read the instructions carefully!

## Task 3: Prototypes
Create constructors, bind methods, and create cuboids in this prototypes challenge.
* [ ] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully!
* [x] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully!

## Task 4: Classes
Once you have completed the prototypes challenge, it's time to convert all your hard work into classes.
* [ ] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully!
* [x] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully!

In your solutions, it is essential that you follow best practices and produce clean and professional results. Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work. It is better to submit a challenge that meets MVP than one that attempts too much and does not.

Expand Down
47 changes: 44 additions & 3 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.
class CuboidMakerClass {
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.width * this.height);
}
}

class CubeMaker extends CuboidMakerClass {
constructor(attrs) {
super(attrs);
}

volume() {
return this.width ** 3;
}

surfaceArea() {
return 6 * this.width ** 2;
}
}

const cuboidClass = new CuboidMakerClass({
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(cuboidClass.volume()); // 100
console.log(cuboidClass.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.
const cube = new CubeMaker({
width: 5
});

// 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()); // 125
console.log(cube.surfaceArea()); // 150
36 changes: 21 additions & 15 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
// ==== Callbacks ====

/* Step 1: Create a higher-order function
* Create a higher-order function named consume with 3 parameters: a, b and cb
* The first two parameters can take any argument (we can pass any value as argument)
* The last parameter accepts a callback
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/

* Create a higher-order function named consume with 3 parameters: a, b and cb
* The first two parameters can take any argument (we can pass any value as argument)
* The last parameter accepts a callback
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/
const consume = (a, b, cb) => 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!"
*/
* 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 = (num1, num2) => num1 + num2;

const multiply = (num1, num2) => num1 * num2;

const greeting = (first, last) => `Hello ${first} ${last}, nice to meet you!`;


/* Step 3: Check your work by un-commenting the following calls to consume(): */
// 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!
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 ====

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

// Explanation:
// Explanation: nestedFunction can access internal due to the creation of a closure. This was needed because internal does not exist within the scope of nestedFunction. The closure gives nestedFunction access to variables that exist in its parent scope.


const external = "I'm outside the function";
Expand All @@ -39,4 +45,4 @@ function myFunction() {
};
nestedFunction();
}
myFunction();
myFunction();
2 changes: 2 additions & 0 deletions challenges/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>

<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -16,4 +17,5 @@
<body>
<h1>Sprint Challenge - Check your work in the console!</h1>
</body>

</html>
Loading