diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 12af878ceb..912a46a2ff 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -17,36 +17,99 @@ 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 = []; -console.log(displayNames); + +const Names = []; +const scienceName = []; + +zooAnimals.forEach(function(animal) { + return Names.push(animal.animal_name) && scienceName.push(animal.scientific_name); +}); + +console.log(`Name: ${Names}, ScientificName: ${scienceName}`); + + + + + + + /* 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 = []; + +zooAnimals.map(function(firstName) { + return lowCaseAnimalNames.push(firstName.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. +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 = []; + +zooAnimals.filter(function(endangered) { + if (endangered.population < 5) { + return lowPopulationAnimals.push(endangered.animal_name); + } +}) + 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. */ let populationTotal = 0; -console.log(populationTotal); + +zooAnimals.reduce(function(callback, 0) { + return populationTotal.push(zooAnimals.population); +}, 0); + + + +console.log(populationTotal(zooAnimals, 0)); + + + + + + + + + + + // ==== Callbacks ==== @@ -58,6 +121,15 @@ console.log(populationTotal); * The consume function should return the invocation of cb, passing a and b into cb as arguments */ +function consume(a, b, callback) { + return callback(a, b); +} + + + + + + /* Step 2: Create several functions to callback with consume(); * Create a function named add that returns the sum of two numbers @@ -65,6 +137,21 @@ 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!" */ +const add = function consume(num1, num2) { + return num1 + num2; +} + +const multiply = function consume(num1, num2) { + return num1 * num2; +} + +const greeting = function consume(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 diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..92f8acf0c2 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,34 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. + +class cube { + CuboidMaker(length, width, height) { + this.length = length; + this.width = width + this.height = height; + } +} + +class volumePro { + CuboidMaker.prototype.volume = function volumeMaker() { + return this.length * this.width * this.height; + } +} + + + Class surfacePro { + CuboidMaker.prototype.surfaceArea = function surfaceAreaMaker() { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) + } +} + + const cuboid = new CuboidMaker(4, 5, 5); + const cuboidOne = 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..aa2ee25e54 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -1,6 +1,6 @@ // ==== Closures ==== -/* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */ +/* Task 1: Study the code below and explain in your own words why nested function can access the variable internal.*/ const external = "I'm outside the function"; @@ -17,8 +17,21 @@ function myFunction() { myFunction(); // Explanation: - +/*Nested Function can access (internal) because it is part of the nested Function scope. Meaning it only lives withing the nested function. */ /* 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 count = 0; + for(let i = 0; i <= num; i++){ + count++; + } return count +}; + + console.log(sumation(4)); + + + + + diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..3bdfca9ad1 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -1,11 +1,18 @@ /* ===== 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(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 +20,14 @@ Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume = function volumeMaker() { + 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 +35,25 @@ Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.surfaceArea = function surfaceAreaMaker() { + 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); +const cuboidOne = 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(cuboidOne.surfaceArea()); // 130