From 11411aba8cceab22375c8841ae0579150be4654e Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 22 May 2020 14:07:31 -0400 Subject: [PATCH 1/3] Sprint --- challenges/arrays-callbacks.js | 94 +++++++++++++++++++++++++++++----- challenges/index.html | 6 +-- challenges/prototypes.js | 27 ++++++++-- 3 files changed, 108 insertions(+), 19 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..f1542f30ac 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -1,6 +1,7 @@ // ==== ADVANCED Array Methods ==== -// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems. +// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in +// the requests below to solve the problems. const zooAnimals = [ { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, @@ -17,35 +18,89 @@ const zooAnimals = [ /* Request 1: .forEach() -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." +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 = []; + +function twoAnimals(zooAnimals, callback) { + // callback(zooAnimals); +} + +const cb = function(zooAnimals) { + return zooAnimals; +} + +zooAnimals.forEach(function(animalName) { + return displayNames.push('Name: ' + animalName.animal_name + ', Scientific Name: ' + animalName.scientific_name); +}); + console.log(displayNames); + + /* Request 2: .map() -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. +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. */ const lowCaseAnimalNames = []; + +function lowCase(zooAnimals, callback2) { + // callback(zooAnimals); +} + +const cb2 = function(zooAnimals) { + return zooAnimals; +} + +zooAnimals.map(function(animalName2) { + return lowCaseAnimalNames.push(animalName2.animal_name + ', '); +}); + 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. +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. */ + +// let game = fifaData.filter(c => (c["Year"] == 2014) && (c["Stage"] == 'Final'))[0]; const lowPopulationAnimals = []; + + let lessthan5 = zooAnimals.filter(less5 => (less5["population"] < 5)); + lowPopulationAnimals.push(lessthan5); + console.log(lowPopulationAnimals); /* 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. +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 = 0; + +function totalPopulation(zooAnimals, callback3) { + // callback(zooAnimals); +} + +const cb3 = function(zooAnimals) { + return zooAnimals; +} + +zooAnimals.reduce(function(accumalator,populations) { + populationTotal = accumalator + populations.population + return populationTotal; +},0); + console.log(populationTotal); @@ -58,24 +113,39 @@ 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 greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" + * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to + * meet you!" */ +const add = function(a, b) { + return a + b; +} -/* 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! - +const multiply = function(a, b) { + return a * b; +} +const greeting = function(a, b) { + return `Hello ${a} ${b}, 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! /* + + Stretch: If you haven't already, convert your array method callbacks into arrow functions. */ diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..211ca136fa 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,10 +7,10 @@ Sprint Challenge - - - + + + diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..cc614fb901 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -1,11 +1,17 @@ /* ===== Prototype Practice ===== */ -// Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are similar to cubes but do not have even sides. Follow the steps in order to accomplish this challenge. +// Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are +// similar to cubes but do not have even sides. Follow the steps in order to accomplish this challenge. /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ +function CuboidMaker(attributes) { + length = attributes.length; + width = attributes.width; + height = attributes.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,21 +19,34 @@ 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. */ +let cuboid = new CuboidMaker ({ + length:4, + width:5, + height: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 From f58be690cf5c7ca815090189fdf1dea5a9a32940 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 22 May 2020 19:20:34 -0400 Subject: [PATCH 2/3] Completed all tasks and questions --- README.md | 14 ++++++++++++++ challenges/arrays-callbacks.js | 27 +-------------------------- challenges/classes.js | 20 ++++++++++++++++++-- challenges/closure.js | 15 ++++++++++++++- challenges/prototypes.js | 14 +++++--------- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index cc6d6902fb..ec33093aad 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,28 @@ 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`: Executes a function once for each array. +`.map`: Creates a new array with the results of calling a provided function on every element in the calling array. + 2. Explain the difference between a callback and a higher order function. +A high-order function (HOF) is a function that returns a function. A callback is a function that gets "called back" by something else once a process is complete. + 3. What is closure? +An inner function has access to out function variables + 4. Describe the four rules of the 'this' keyword. +Whenever a function is contained in the global scope, the value of `this` inside of that function will be the window object. +`this` is applied to the left of the dot. +Whenever a constructor function is used, `this` refers to the specific instance of the object that is created and returned by the constructor function. +Whenever JavaScript’s call or apply method is used, `this` is explicitly defined. + 5. Why do we need super() in an extended class? +The `super` keyword in JavaScript is used in order to call the methods of the parent 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 f1542f30ac..93d27a9478 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -25,14 +25,6 @@ string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aur */ const displayNames = []; -function twoAnimals(zooAnimals, callback) { - // callback(zooAnimals); -} - -const cb = function(zooAnimals) { - return zooAnimals; -} - zooAnimals.forEach(function(animalName) { return displayNames.push('Name: ' + animalName.animal_name + ', Scientific Name: ' + animalName.scientific_name); }); @@ -50,14 +42,6 @@ of strings named lowCaseAnimalNames, each string following this pattern: "jackal const lowCaseAnimalNames = []; -function lowCase(zooAnimals, callback2) { - // callback(zooAnimals); -} - -const cb2 = function(zooAnimals) { - return zooAnimals; -} - zooAnimals.map(function(animalName2) { return lowCaseAnimalNames.push(animalName2.animal_name + ', '); }); @@ -71,7 +55,6 @@ lowPopulationAnimals which contains only the animals with a population less than */ -// let game = fifaData.filter(c => (c["Year"] == 2014) && (c["Stage"] == 'Final'))[0]; const lowPopulationAnimals = []; let lessthan5 = zooAnimals.filter(less5 => (less5["population"] < 5)); @@ -86,15 +69,7 @@ using the .reduce() method. Remember the reduce method takes two arguments: a ca an initial value for the count. */ -//const populationTotal = 0; - -function totalPopulation(zooAnimals, callback3) { - // callback(zooAnimals); -} - -const cb3 = function(zooAnimals) { - return zooAnimals; -} +let populationTotal = 0; zooAnimals.reduce(function(accumalator,populations) { populationTotal = accumalator + populations.population diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..2440ae8223 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 CuboidMaker2 { + constructor(length2,width,height) { + this.length2 = length2; + this.width = width; + this.height = height; + } + volume() { + return this.length2 * this.width * this.height; + } + surfaceArea() { + return 2 * (this.length2 * this.width + this.length2 * this.height + this.width * this.height); + } +} + +let cuboid2 = new CuboidMaker2(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(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. \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..df7be3339e 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -2,6 +2,8 @@ /* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ +// Variables are scoped to the parent function. Because internal is set inside of the parent function: myFunction(), +// nestedFunction() - the child, can access and call the variable from it's parent. const external = "I'm outside the function"; @@ -21,4 +23,15 @@ myFunction(); /* 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. */ +/* 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 sumation = function(num) { + let total = 0; + for(let i = 0; i <= num; i++){ + total += i; + } + console.log('Summation: ' + total); +} + +sumation(6); // Returns 21 \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index cc614fb901..315c9d46f7 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -7,10 +7,10 @@ Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ -function CuboidMaker(attributes) { - length = attributes.length; - width = attributes.width; - height = attributes.height; +function CuboidMaker(length,width,height) { + this.length = length; + this.width = width; + this.height = height; } /* == Step 2: Volume Method == @@ -39,11 +39,7 @@ CuboidMaker.prototype.surfaceArea = function() { Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ -let cuboid = new CuboidMaker ({ - length:4, - width:5, - height:5 -}); +let cuboid = new CuboidMaker (4,5,5); // Test your volume and surfaceArea methods by uncommenting the logs below: console.log(cuboid.volume()); // 100 From f22b08fd141b88d1f7740b4f5cf187bd598e7068 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 22 May 2020 20:03:43 -0400 Subject: [PATCH 3/3] moved explanation down on closure.js --- challenges/closure.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenges/closure.js b/challenges/closure.js index df7be3339e..6af3c1a6b1 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -2,9 +2,6 @@ /* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ -// Variables are scoped to the parent function. Because internal is set inside of the parent function: myFunction(), -// nestedFunction() - the child, can access and call the variable from it's parent. - const external = "I'm outside the function"; function myFunction() { @@ -20,6 +17,8 @@ myFunction(); // Explanation: +// Variables are scoped to the parent function. Because internal is set inside of the parent function: myFunction(), +// nestedFunction() - the child, can access and call the variable from it's parent. /* Task 2: Counter */