diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..a6a1e8b2ee 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((arrItem) => { + return displayNames.push(`Name: ${arrItem.animal_name}, Scientific: ${arrItem.scientific_name}`); +}); console.log(displayNames); /* Request 2: .map() @@ -29,7 +32,9 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map((arrItem) => { + return arrItem.animal_name.toLowerCase(); +}); console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -38,6 +43,11 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; +zooAnimals.filter((arrItem) => { + if(arrItem.population < 5) { + lowPopulationAnimals.push(arrItem); + } +}); console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,7 +55,9 @@ 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; +const populationTotal = zooAnimals.reduce((accum, item) => { + return accum + item.population; +},0); console.log(populationTotal); @@ -57,21 +69,33 @@ console.log(populationTotal); * The last parameter accepts a callback * 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!" */ +const add = function(num1, num2) { + return num1 + num2; +} +const multiply = function(num1, num2) { + return num1 * num2; +} -/* 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 greeting = function(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 +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..bef0546c62 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,22 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker { + constructor(length, width, height) { + this.length = length; + this.width = width; + this.height = height; + } + volume(length, width, height) { + return this.length * this.width * this.height; + } + surfaceArea(length, width, height) { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) + } + } + + 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 // 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..ed7cf22576 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -16,9 +16,21 @@ function myFunction() { } myFunction(); -// Explanation: +// Explanation: because of how scope works. Inside the scope of nestedFunction I can access everything outside of it. But I cannot access variables unique to nested Functions scope from outside /* 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 (i = 0; i <= num; i++) { + result += i; + } + return result +} + +console.log(sumation(4)) \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..369a35d5f6 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -5,7 +5,11 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ - +const CuboidMaker = function(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 +17,9 @@ Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume = function(length, width, height) { + 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 +27,19 @@ Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.surfaceArea = function(length, width, height) { + 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 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..48e341a095 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +}