diff --git a/README.md b/README.md index cc6d6902fb..e15011636d 100644 --- a/README.md +++ b/README.md @@ -26,27 +26,41 @@ Edit this document to include your answers after each question. Make sure to lea 1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) +forEach and map both access every element in an array. map returns a new array that contains whatever you return. forEach does not return an array and can be used to modify the original array. + 2. Explain the difference between a callback and a higher order function. +A higher order function is a function that takes another function as an argument. A callback is a function that is passed into a higher order function. + 3. What is closure? +A closure is a function that is enclosed with references to its state. This allows for a state to persist between runs of a function. + 4. Describe the four rules of the 'this' keyword. +1. this in the global scope refers to the window/console object +2. When a preceding dot calls a function, the object before the dot is this +3. When a constructor function is used, this refers to the specific instance of the object that is created and returned +4. When call or apply is used, then this is explicitly defined + + 5. Why do we need super() in an extended class? +super allows us to pass the variables to the parent class, since we are using a child or extended class. + ### Task 1 - Project Set up Follow these steps to set up and work on your project: Make sure you clone the branch that the TK links to: the vnext branch, NOT master! -- [ ] 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 ``. -- [ ] 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 ``. +- [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 ``. +- [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 ``. @@ -59,22 +73,22 @@ Your finished project must include all of the following requirements: #### Task A: Objects and Arrays Test your knowledge of advanced array methods and callbacks. -* [ ] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully! +* [x] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully! #### Task B: Closure -This challenge takes a look at closures as well as scope. -* [ ] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully! +This challenge takes a look at closures as well as scope. +* [x] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully! #### Task C: 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 D: 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. @@ -86,6 +100,6 @@ There are a few stretch problems found throughout the files, don't work on them Follow these steps for completing your project: -- [ ] Submit a Pull-Request to merge 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. +- [x] Submit a Pull-Request to merge Branch into master (student's Repo). +- [x] Add your team lead as a Reviewer on the Pull-request +- [x] TL then will count the HW as done by merging the branch back into master. diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..c8b87d0c86 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,7 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +zooAnimals.forEach(animal => displayNames.push(`Name: ${animal.animal_name}, Scientific: ${animal.scientific_name}`)); console.log(displayNames); /* Request 2: .map() @@ -29,27 +30,27 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map(animal => animal.animal_name.toLowerCase()); console.log(lowCaseAnimalNames); -/* Request 3: .filter() +/* Request 3: .filter() The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5. */ -const lowPopulationAnimals = []; +const lowPopulationAnimals = zooAnimals.filter(animal => animal.population < 5); console.log(lowPopulationAnimals); -/* Request 4: .reduce() +/* Request 4: .reduce() The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count. */ -const populationTotal = 0; +const populationTotal = zooAnimals.reduce((animalAccumulator, animal) => animalAccumulator + animal.population, 0); console.log(populationTotal); -// ==== Callbacks ==== +// ==== Callbacks ==== /* Step 1: Create a higher-order function * Create a higher-order function named consume with 3 parameters: a, b and cb @@ -58,18 +59,31 @@ console.log(populationTotal); * 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 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! diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..f59ae532ac 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,47 @@ // 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. + +class CubeMaker extends CuboidMaker { + constructor(length) { + super(length, length, length); + } + + volume() { + return Math.pow(this.length, 3); + } + + surfaceArea() { + return 6 * Math.pow(this.length, 2); + } +} + +const cube = new CubeMaker(2); -// 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 +console.log(cube.volume()); // 8 +console.log(cube.surfaceArea()); // 24 \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..1d14a34d4f 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -1,4 +1,4 @@ -// ==== Closures ==== +// ==== Closures ==== /* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ @@ -16,9 +16,22 @@ function myFunction() { } myFunction(); -// Explanation: +// Explanation: +// The variable internal is in the same lexical scope as nestedFunction. nestedFunction has access to all the variables that are within the global scope and the myFunction scope. /* Task 2: Counter */ /* Create a function called `sumation` that accepts a parameter and uses a counter to return the summation of that number. For example, `summation(4)` should return 10 because 1+2+3+4 is 10. */ + +function summation(number) { + let count = 0; + + for(let i=1; i<=number; i++) { + count += i; + } + + return count; +} + +console.log(summation(4)); \ No newline at end of file diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..0dc5ec27dd 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,8 +7,8 @@ Sprint Challenge - - + + diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..cdeec32f30 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,28 +6,41 @@ 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. + 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. + 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