diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..68167cf6f0 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "workbench.colorCustomizations": { + "activityBar.background": "#1A3322", + "titleBar.activeBackground": "#244730", + "titleBar.activeForeground": "#F8FCF9" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 408612117c..1f39ab488e 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,30 @@ Edit this document to include your answers after each question. Make sure to lea 1. Describe the biggest difference between `.forEach` & `.map`. + ~ The biggest differnce to me is that .forEach() returns undefined whereas, .map() returns a new array. + 2. What is the difference between a function and a method? + ~ functions are a set a statements that preforms a task whereas, a method is a function which belongs to a object. + 3. What is closure? + ~ A closure gives you access to an outer function scope from an inner function. + 4. Describe the four rules of the 'this' keyword. + A. "This" will refer to the window/global object if not implicitly or explicitly binded. + + B. "This" will be binded to the object which a method was called from. (Implicit binding) + + C. When using constructors, "This" will be binded to what ever object was created using the "new" keyword. + + D. You can explicitly bind "This" using any of these methods: .bind(), .call(), .apply() + 5. Why do we need super() in an extended class? + ~ We need super()because it makes the child constructor aware of the parent prototype. + ## Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..35aaad5cce 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,23 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker{ + constructor(length,width, height) + this.length = length; + this.width = width; + this.height = 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(4,5,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. \ No newline at end of file diff --git a/challenges/functions.js b/challenges/functions.js index 6e3688bfcc..1d2bac38c7 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -6,19 +6,31 @@ * The last parameter accepts a callback * The consume function should return the invocation of cb, passing a and b into cb as arguments */ - +function consume(a,b,cb){ + return 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; +} +function multiply(a,b){ + return a * b; +} + +function greeting(firstName, lastName){ + return `Hello ${firstName} ${lastName}, 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 ==== @@ -27,6 +39,9 @@ // Explanation: +// It begins by serching for the variable "internal" inside the nestedFunction's scope. +// since "internal" isn't there, JavaScript sees "nestedfunction" which is part of the outter function scope. + const external = "I'm outside the function"; diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..6189e4fa71 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,14 +5,20 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ - +function CuboidMaker(length, width, height){ + this.length = length; + this.width = width; + this.height = 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. @@ -20,14 +26,19 @@ 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(4,5,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