diff --git a/challenges/answers.txt b/challenges/answers.txt new file mode 100644 index 0000000000..b852fea6ae --- /dev/null +++ b/challenges/answers.txt @@ -0,0 +1,23 @@ +1. Briefly compare and contrast .forEach & .map (2-3 sentences max) + +The .forEach method calls a function once for each array element. Contrast this with the .map method which creates a new array with the same elements of the array you are calling in the method. In other words, .map will copy the elements of an array over to a new array. + +2. Explain the difference between a callback and a higher order function. + +The difference between a callback and a higher order function is the way that the function is passed through the anonymous function in your code. For example a callback function is a function passed into another function as an argument, which is called inside the outer function to do a job. While a higher order function has a parent child relationship with the functions they are trying to call. + +3. What is closure? + +Closure in javascript is how to define what scope a variable or function will be called in. These scopes include local scope, it has access to the outer function, and global variables. So this is analogous to the nesting in less CSS with the closures dictating the parent child relationship with the + +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. +-Whenever a function is called by a preceding dot, the object before that dot is this. +-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? + +Super() is used to call the functions of a parent of a higher order object. diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..1d90d5f526 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -3,16 +3,16 @@ // 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" }, - { animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" }, - { animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" }, - { animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" }, - { animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" }, - { animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" }, - { animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" }, - { animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" }, - { animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" }, - { animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" }, + { animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" }, + { animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" }, + { animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" }, + { animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" }, + { animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" }, + { animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" }, + { animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" }, + { animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" }, + { animal_name: "Hawk-eagle, crowned", population: 10, scientific_name: "Spizaetus coronatus", state: "Florida" }, + { animal_name: "Australian pelican", population: 5, scientific_name: "Pelecanus conspicillatus", state: "West Virginia" }, ]; /* Request 1: .forEach() @@ -20,7 +20,8 @@ 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 = []; +let displayNames = []; +displayNames = zooAnimals.forEach(animal => animal.population.state.slice()); console.log(displayNames); /* Request 2: .map() @@ -29,15 +30,17 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; -console.log(lowCaseAnimalNames); +let lowerCase = []; +lowerCase = zooAnimals.map(animal => animal.animal_name.toLowerCase()); +console.log(lowerCase); /* 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 = []; +let lowPopulationAnimals = []; +lowPopulationAnimals = zooAnimals.filter(animal => animal.population.length > 5) console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,31 +48,49 @@ 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. */ -const populationTotal = 0; + +let populationTotal = []; +populationTotal = zooAnimals.reduce((acc, animal) => { return acc += animal.population }, 0); console.log(populationTotal); // ==== Callbacks ==== /* Step 1: Create a higher-order function - * Create a higher-order function named consume with 3 parameters: a, b and cb - * The first two parameters can take any argument (we can pass any value as argument) - * The last parameter accepts a callback - * The consume function should return the invocation of cb, passing a and b into cb as arguments -*/ + * Create a higher-order function named consume with 3 parameters: a, b and cb + * The first two parameters can take any argument (we can pass any value as argument) + * The last parameter accepts a callback + * The consume function should return the invocation of cb, passing a and b into cb as arguments + */ + +function consume(a, b, cd) { + return cd(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 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!" + */ + +function add(num1, num2) { + return num1 + num2; +} +function multiply(num1, num2) { + return num1 * num2; +} + +function greeting(first, last) { + return `Hello ${first} ${last}, 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! + @@ -78,5 +99,4 @@ console.log(populationTotal); Stretch: If you haven't already, convert your array method callbacks into arrow functions. -*/ - +*/ \ No newline at end of file diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..38ed56236e 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,27 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker { + constructor(params) { + this.length = params.length; + this.width = params.width; + this.height = params.height; + } + volume() { + return this.length * this.width * this.height; + } + surfaceArea() { + return 2 * ((this.length * this.width) + (this.length * this.height) + (this.width * this.height)); + } +} + +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 // 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/prototypes.js b/challenges/prototypes.js index 4cafc33e95..d990a94673 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,13 +5,21 @@ /* == Step 1: Base Constructor == 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 == @@ -19,15 +27,18 @@ 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. */ -// Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +let 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 \ No newline at end of file