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
36 changes: 36 additions & 0 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`.

Both .forEach and .map take a function as a parameter and apply it
it to each element of an array. However, .map will automatically
store the results from each element into a new array of the same
length, while forEach does not. When using forEach it is necessary
for the programmer to explicitly handle this, such as by storing results
into an object created outside the scope of the forEach.

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

A method is a function that belongs to an object (either by having
been explicitly defined inside the object or by belonging to the
prototype of an object) and is called using dot notation. A plain
function is invoked directly without being attached to an object via
dot notation.

3. What is closure?

When a function is defined inside of another function, the inner
function carries with it the environment of the outer function
including variables created in the outer function's scope, even
after the outer function has exited.

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

The four types of binding for the 'this' keyword are:

A. Window Binding: When there is no other binding (i.e., when in the global scope),
the default binding for 'this' is the window (or console if running Node)

B. Implicit Binding: When calling a method belonging to an object using dot notation,
'this' is implicitly bound to the object to the left of the dot.

C. New Binding: When the 'new' keyword is used to instantiate a class with a constructor,
memory is allocated for the object being created and 'this' is bound to the new object.

D. Explicit Binding: When a function is invoked by using the call, apply, or bind methods,
a context is passed as a parameter and 'this' is bound to that context.

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

This is necessary to invoke the constructor of the parent class,
which will not happen automatically if we don't call super().


## Project Set up

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

"use strict";


class CuboidMaker {
constructor(props) {
this.length = props.length;
this.width = props.width;
this.height = props.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.


// Since Cubemaker only has a single property, we will pass its value directly
// instead of making it a member of a props object. The parent's volume and surfaceArea
// methods will continue to work as is, so no changes needed (that's the whole point
// of inheritence, right?)

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


const cubeyMcCubeFace = new CubeMaker(5);

console.log(`I am a cube of size ${cubeyMcCubeFace.size} units.`);
console.log(`My volume is ${cubeyMcCubeFace.volume()} units cubed.`);
console.log(`My surface area is ${cubeyMcCubeFace.surfaceArea()} units squared.`);

// 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.
42 changes: 35 additions & 7 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
// ==== Callbacks ====

"use strict";

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

const 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 greeting that accepts a first and last name and returns
"Hello first-name last-name, nice to meet you!"
*/

const add = (a, b) => a + b;

const multiply = (a, b) => a * b;

const greeting = (fn, ln) => `Hello ${fn} ${ln}, 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 ====
Expand All @@ -27,16 +40,31 @@

// Explanation:

// When nestedfunction is created, its closure includes variables defined in
// the outer environment that nestedfunction uses -- which in this case is just
// the variable 'internal'. It's not really illustrated by this particular
// example, but even after myFunction goes out of scope, if we still had access
// to nestedFunction it would still be able to access its copy of 'internal'

// actually, let's see that in action...

const external = "I'm outside the function";
const internal = "My name is a lie!";

function myFunction() {
console.log(external);
const internal = "Hello! I'm inside myFunction!";

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

return nestedFunction;
}
myFunction();


const nested = myFunction();

console.log(internal);

nested();
4 changes: 2 additions & 2 deletions challenges/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<title>Sprint Challenge</title>

<script src="objects-arrays.js"></script>
<!-- <script src="objects-arrays.js"></script>
<script src="functions.js"></script>
<script src="prototypes.js"></script>
<script src="prototypes.js"></script> -->
<script src="classes.js"></script>
</head>

Expand Down
Loading