diff --git a/README.md b/README.md index cc6d6902fb..42659cbd9d 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,29 @@ 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) +-.map will return a new array while .forEach doesn't mutate the array. + 2. Explain the difference between a callback and a higher order function. +-A higher order function takes another function as an argument and will return a funtion. +-A callback function is passed through a function and the function that it's passed through will call it. + 3. What is closure? +-Closure is a how a function is bundled together, a closure is created everytime a function is created. Aka it's lexical environment. + 4. Describe the four rules of the 'this' keyword. +-Window/Global binding: a this that is placed in the global scope. +-Implicit binding: use implicit binding when dot notation is used to invoke a function. +-Explicit binding: using .call and .apply where this is explicity defined. +-New: refers to the specific instance of the object that is created and returned by the new binding. + 5. Why do we need super() in an extended class? +-Super is used to call the parent class and to access the parent's properties. + + ### Task 1 - Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..6cafdfc186 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -20,7 +20,13 @@ const zooAnimals = [ The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus." */ +console.log("===REQUEST 1===") + const displayNames = []; +zooAnimals.forEach((item) => { + displayNames.push(`Name: ${item.animal_name}, Scientific: ${item.scientific_name}`) +}) + console.log(displayNames); /* Request 2: .map() @@ -28,8 +34,13 @@ console.log(displayNames); The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut. */ +console.log("===REQUEST 2===") const lowCaseAnimalNames = []; +zooAnimals.map((item) => { + lowCaseAnimalNames.push(item.animal_name.toLocaleLowerCase()) +}) + console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -37,7 +48,16 @@ console.log(lowCaseAnimalNames); 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. */ +console.log("===REQUEST 3===") + const lowPopulationAnimals = []; + +zooAnimals.filter((item) => { + if(item.population < 5) { + lowPopulationAnimals.push(item); + } +}) + console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,7 +65,20 @@ console.log(lowPopulationAnimals); 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. */ +console.log("===REQUEST 4===") + let populationTotal = 0; +new_array = []; + +zooAnimals.forEach((item) => { + new_array.push(item.population); +}) + +new_array.reduce((a,b) => { + populationTotal = a + b; + return populationTotal; +},0) + console.log(populationTotal); @@ -58,6 +91,10 @@ 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 @@ -65,13 +102,26 @@ console.log(populationTotal); * 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("Step 1:", consume(2, 2, add)); +console.log("Step 2:", consume(10, 16, multiply)); +console.log("Step 2:", consume("Mary", "Poppins", greeting)); /* diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..8fcb9a77c9 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/closure.js b/challenges/closure.js index 101d68e553..f7f3d9ad39 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -16,9 +16,21 @@ function myFunction() { } myFunction(); -// Explanation: +// Explanation: nested function can access const external bc external is in the global scope, any function can reach up to the global scope and grab a defined variable. /* 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. */ + +console.log("===Task 2===") + +function sumation(num) { + let counter = 0; + for (let i = 0; i <= num; i++) { + counter += i; + } + return counter; +} + +console.log(sumation(4)); \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..f38c1446de 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,6 +6,12 @@ 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 @@ -13,6 +19,10 @@ Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume = function() { + const volumeFormula = this.length * this.width * this.height; + return volumeFormula; +} /* == 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 +30,22 @@ Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.surfaceArea = function(){ + const areaFormula = 2 * (this.length * this.width + this.length * this.height + this.width * this.height); + return areaFormula; +} + + /* == 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