diff --git a/README.md b/README.md index cc6d6902fb..b89f724353 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) +They are pretty close to the same. I think the main difference is that `.map` automatically returns a new 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 function is a function that is passed to another function with the expectation that the other function will call it. + 3. What is closure? +closures are created every time a function is created + 4. Describe the four rules of the 'this' keyword. +Is the function called by new ? +Is the function called by call() , apply() , or bind() ? +Is the function called as a method, ie: obj. func() ? +Is the function called in the global scope? If strict mode is enabled, return undefined . Otherwise, return the global object, ie: window . + 5. Why do we need super() in an extended class? +The super keyword refers to the parent class. It is used to call the constructor of the parent class and to access the parent's properties and methods. + ### 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..a3ce4af23b 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,9 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +zooAnimals.forEach(element => { +displayNames.push('Name: ' + element.animal_name + ', Scientific: ' + element.scientific_name + '.'); +}) console.log(displayNames); /* Request 2: .map() @@ -30,6 +33,7 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +lowCaseAnimalNames.push(zooAnimals.map( element => element.animal_name.toLowerCase())); console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -38,6 +42,7 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; +lowPopulationAnimals.push(zooAnimals.filter(elelment => elelment.population < 5)); console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -46,6 +51,7 @@ The zoos need to know their total animal population across the United States. Fi */ let populationTotal = 0; +populationTotal = zooAnimals.reduce(((a,b) => a + b.population), 0); console.log(populationTotal); @@ -58,18 +64,23 @@ console.log(populationTotal); * The consume function should return the invocation of cb, passing a and b into cb as arguments */ +const consume = function(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!" */ - +let add = (a,b) => a + b; +let multiply = (a,b) => a * b; +let greeting = (a,b) => 'Hello ' + a + ' ' + b + ', nie 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..13867c5ef3 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,29 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. - +class CuboidMaker2{ + 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)} +} // Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +const cuboid2 = new CuboidMaker2(4, 5, 5); +console.log(cuboid2.volume()); // 100 +console.log(cuboid2.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. + +// well this is silly. Guess what: the same formulas work. They are basically the same shape but the sides are even. + +class CubeMaker extends CuboidMaker2{ + constructor(length, width, height){ + super(length, width, height); + } +} + +const cube = new CubeMaker(5,5,5); -// 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()); +console.log(cube.surfaceArea()); \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..13d1e43361 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -17,8 +17,19 @@ function myFunction() { myFunction(); // Explanation: +// since internal was declared within the scope of myFunction(), it can be accessed anywhere within in that scome. nestedFunction is within the scope of myFunction() and can therefore access the 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. */ + +const summation = function(num){ + let answer = 0; + for(let i = 1; i <= num; i++){ + answer = answer + i; + } + return answer; +} + +console.log(summation(4)); \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..0f9b192f9d 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,6 +6,11 @@ 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 +18,7 @@ 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,16 @@ 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