diff --git a/README.md b/README.md index cc6d6902fb..22863217be 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,27 @@ 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 does not modify an array while .map does modify an array. + 2. Explain the difference between a callback and a higher order function. +- + 3. What is closure? +-Closure is when an inside function can access the outside function’s variables. + 4. Describe the four rules of the 'this' keyword. + 1. Window/Global Object Binding - references the window object - this is "the whole forest." + 2. Implicit Binding - this refers to the odject before the dot when a function is called. + 3. New binding - this refers to the object created using the construcor function. + 4. Explicit binding - this is defined by using the apply or call method. + 5. Why do we need super() in an extended class? +-super() allows access to the this items from the extended class. + ### 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 472ab3e96d..9f562c3109 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -20,7 +20,10 @@ 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." */ -const displayNames = []; +const displayNames = [] +zooAnimals.forEach((name) => { + displayNames.push(`animal_name: ${name.animal_name}, scientific_name: ${name.scientific_name}.`); +}) console.log(displayNames); /* Request 2: .map() @@ -30,14 +33,20 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +zooAnimals.map((name) => { + lowCaseAnimalNames.push( name.animal_name.toLowerCase()); +}); console.log(lowCaseAnimalNames); + /* 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((data) => { + return data.population > 6 +}); console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -46,6 +55,7 @@ The zoos need to know their total animal population across the United States. Fi */ const populationTotal = 0; +zooAnimals.reduce((total,animal) => total + animal.population,0); console.log(populationTotal); @@ -58,6 +68,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,11 +79,20 @@ 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(cb1,cb2){ + return cb1 + cb2; +}; +function multiply(cb1,cb2){ + return cb1*cb2; +}; +function greeting(cb1,cb2){ + return `Hello ${cb1} ${cb2}, 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..3ba6716f7d 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(cuboidMakerAttributes){ + this.length = cuboidMakerAttributes.length; + this.width = cuboidMakerAttributes.width; + this.height = cuboidMakerAttributes.height; + } + volume() { + return this.length * this.width * this.height; + } + surfaceArea() { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); + } + } -// Test your volume and surfaceArea methods by uncommenting the logs below: -// 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 + const cuboid = new CuboidMaker({ + length: 4, + width: 5, + height: 5 + }); + + + 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(cubeMakerAttributes) { + super(cubeMakerAttributes); + } + cVolume() { + return Math.pow(this.length, 3); + } + cSurfaceArea() { + return 6 * (Math.pow(this.length, 2)); + } +} + + +const cube = new CubeMaker({ + length: 2, +}); + +console.log(cube.cVolume()); +console.log(cube.cSurfaceArea()) \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..ae72102c81 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -17,8 +17,18 @@ function myFunction() { myFunction(); // Explanation: - +//Internal is located within the nested function, so it is within the scope, therefore can be accessed. /* 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 sumation(num) { + let result = 0; + for (var i = 1; i <= num; i++) { + result += i; + } + return result; +} + +let output = sumation(4); +console.log(output); \ No newline at end of file diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..e13045219b 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,10 +7,10 @@