From bf43bef3cd3459939df064d360714e2bf2290602 Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 12:52:30 -0400 Subject: [PATCH 1/7] Completed Arrays-callbacks and Closure --- challenges/arrays-callbacks.js | 63 ++++++++++++++++++++++++++++++++-- challenges/closure.js | 15 ++++++++ challenges/index.html | 4 +-- 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..2b1a380889 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -23,6 +23,22 @@ The zoos want to display both the scientific name and the animal name in front o const displayNames = []; console.log(displayNames); + +zooAnimals.forEach(function(array){ + displayNames.push(`Name: ${array["animal_name"]}`); + return displayNames +}) + + zooAnimals.forEach(function(array){ + displayNames.push(`Scientific: ${array["scientific_name"]}`); + return displayNames + }) + + + + +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. @@ -30,7 +46,14 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; -console.log(lowCaseAnimalNames); + + +zooAnimals.forEach(function(array){ + lowCaseAnimalNames.push(array["animal_name"].toLowerCase()); + return lowCaseAnimalNames; + }) + + console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -38,6 +61,15 @@ The zoos are concerned about animals with a lower population count. Using filter */ const lowPopulationAnimals = []; + +zooAnimals.filter(function(array){ + if(array["population"]>5){ + lowPopulationAnimals.push(array["population"]); + } + return lowPopulationAnimals; + }) + + console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,10 +77,16 @@ 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 = 0; + +populationTotal=zooAnimals.reduce(function (accumulator, item) { + return accumulator + item['population']; +}, 0) + console.log(populationTotal); + // ==== Callbacks ==== /* Step 1: Create a higher-order function @@ -59,19 +97,38 @@ console.log(populationTotal); */ + +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!" */ +const multiply = (a, b) => a * b; +const divide = (a, b) => a / b; +const add = (a, b) => a + b; +const greeting = (a, b) => `Hello ${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! - +console.log(consume(2, 4,add)); +console.log(consume(2, 10,divide)); +console.log(consume(2, 20,multiply)); +console.log(greeting("Cameron", "Lares",greeting)); /* diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..4bf36eabea 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -18,7 +18,22 @@ myFunction(); // Explanation: +//Internal is inside its parent scope 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. */ + + + + +function add(count){ + let counter=0; +for(let i=0; i<=count; i++){ +counter+=i; +} +// For each increment add 1 to last +return counter; +} + +console.log(add(4)) \ No newline at end of file diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..fe5e0e430c 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,8 +7,8 @@ Sprint Challenge - - + + From dd27de2d3a9f82a8cb19ad12234def363fee3943 Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 13:01:22 -0400 Subject: [PATCH 2/7] Completed Arrays-callbacks and Closure & Prototype --- challenges/prototypes.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..444246f27b 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,6 +6,13 @@ 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,11 @@ 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 +32,21 @@ 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 From b7eea46da3286fa7fd472c3d6c130a652a6d9229 Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 13:31:09 -0400 Subject: [PATCH 3/7] Completed Arrays-callbacks, Cloures, Prototype, and Classes. Completed MVP --- challenges/classes.js | 48 +++++++++++++++++++++++++++++++++++++++++-- challenges/closure.js | 4 ++-- challenges/index.html | 4 ++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..521f5bfde2 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,51 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +// function CuboidMaker(length,width,height){ +// this.length=length; +// this.width=width; +// this.height=height; + +// } + +// CuboidMaker.prototype.volume= function(){ +// return this.length * this.width * this.height; +// } + +// CuboidMaker.prototype.surfaceArea= function(){ +// return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) +// } + + +// const cuboid = new CuboidMaker(4,5,5); + + + +class newCuboidMaker{ + constructor(attributes) { + this.length=attributes.length; + this.width=attributes.width; + this.height=attributes.height; + } + //Special Methods + + volume(){ + return this.length * this.width * this.height + } + surfaceArea (){ + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) + } + + } //End of CuboidMaker + + const Classcuboid = new newCuboidMaker({ + 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(Classcuboid.volume()); // 100 +console.log(Classcuboid.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 4bf36eabea..7d07e09dc3 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -27,7 +27,7 @@ myFunction(); -function add(count){ +function sumation(count){ let counter=0; for(let i=0; i<=count; i++){ counter+=i; @@ -36,4 +36,4 @@ counter+=i; return counter; } -console.log(add(4)) \ No newline at end of file +console.log(sumation(4)) \ No newline at end of file diff --git a/challenges/index.html b/challenges/index.html index fe5e0e430c..0dc5ec27dd 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,8 +7,8 @@ Sprint Challenge - - + + From 861155abf8bcf47443fffea15ede2474620dae19 Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 14:24:12 -0400 Subject: [PATCH 4/7] Completed Arrays-callbacks, Cloures, Prototype, and Classes. Completed MVP & Stretch --- challenges/classes.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/challenges/classes.js b/challenges/classes.js index 521f5bfde2..69c1a96fa7 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -48,4 +48,23 @@ class newCuboidMaker{ console.log(Classcuboid.volume()); // 100 console.log(Classcuboid.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 +// 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. + +class CubeMaker extends newCuboidMaker{ + constructor(attributes){ + super(attributes) + + + +}//End of Attributes +//Special Methods +} + +const Cube = new newCuboidMaker({ + length:5, + width:6, + height:6, +}); + +console.log(`Stretch Child Cube ${Cube.volume()}`); +console.log(`Stretch Child Cube ${Cube.surfaceArea()}`); \ No newline at end of file From b5d0c7b65cfcfee40667c3623cd27c9d373b60af Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 14:30:37 -0400 Subject: [PATCH 5/7] Completed Arrays-callbacks, Cloures, Prototype, and Classes. Completed MVP & Stretch Corrected --- challenges/arrays-callbacks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 2b1a380889..81efc6e5bd 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -63,7 +63,7 @@ The zoos are concerned about animals with a lower population count. Using filter const lowPopulationAnimals = []; zooAnimals.filter(function(array){ - if(array["population"]>5){ + if(array["population"]<5){ lowPopulationAnimals.push(array["population"]); } return lowPopulationAnimals; From 8ed06f02d7057701805a3ef57103fc0f575b341a Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 14:31:27 -0400 Subject: [PATCH 6/7] Completed Arrays-callbacks, Cloures, Prototype, and Classes. Completed MVP & Stretch Corrected_ --- challenges/arrays-callbacks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 81efc6e5bd..117cea7763 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -126,7 +126,7 @@ const greeting = (a, b) => `Hello ${a} ${b}`; // console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! console.log(consume(2, 4,add)); -console.log(consume(2, 10,divide)); +console.log(consume(10, 2,divide)); console.log(consume(2, 20,multiply)); console.log(greeting("Cameron", "Lares",greeting)); From f05d935262c6eb0eafbf82aa0ae32689f2424317 Mon Sep 17 00:00:00 2001 From: Cameron Lares Date: Fri, 22 May 2020 14:32:11 -0400 Subject: [PATCH 7/7] Completed Arrays-callbacks, Cloures, Prototype, and Classes. Completed MVP & Stretch Corrected_1 --- challenges/arrays-callbacks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 117cea7763..3b56081a33 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -128,7 +128,7 @@ const greeting = (a, b) => `Hello ${a} ${b}`; console.log(consume(2, 4,add)); console.log(consume(10, 2,divide)); console.log(consume(2, 20,multiply)); -console.log(greeting("Cameron", "Lares",greeting)); +console.log(greeting("Cameron", "Lares, nice to meet you!",greeting)); /*