From b55ba42e830f2ee9a9b281692c3262410c4912c3 Mon Sep 17 00:00:00 2001 From: Christopher-Martinez-422 <107010274+Christopher-Martinez-422@users.noreply.github.com> Date: Sat, 3 Sep 2022 07:21:01 -0700 Subject: [PATCH] first commit --- README.md | 15 ++++++++ index.js | 111 +++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 95 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index fca26a9e3..6bacf0277 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,29 @@ Demonstrate your understanding of this week's concepts by answering the followin Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read. 1. Explain the differences between `.map`, `.reduce` and `.filter` and describe a use case for each. + 2. Explain the difference between a callback and a higher order function. + 3. Explain what a closure is. + 4. Describe the four principles of the 'this' keyword. + 5. Why do we need super() in an extended class? + You are expected to be able to answer questions in these areas. Your responses contribute to your Sprint Challenge grade. diff --git a/index.js b/index.js index e37f41ec7..08b66e4fe 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ function myFunction() { //πŸš€πŸš€πŸš€ ⬇️ πŸ“ Explanation ⬇️ πŸ“ πŸš€πŸš€πŸš€: - +// nestedFunction can access internal because it is within the scope of myFunction. @@ -30,10 +30,13 @@ function myFunction() { πŸ’‘ NOTE: you may use a for loop for this function if you wish */ -function summation(/*Your Code Here*/) { - /*Your Code Here*/ - +function summation(number) { + let sum = 0; + for (let i = 1; i <= number; i++) { + sum += i; } + return sum; +} // 🦁🦁🦁 Topic 2: ADVANCED Array Methods 🦁🦁🦁 @@ -60,8 +63,12 @@ const zooAnimals = [ πŸ’‘ NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}" */ - function animalNames(/*Your Code Here*/){ - /*Your Code Here*/ + function animalNames(array){ + const displayNames = []; + array.forEach(function(item, index){ + displayNames.push(`name: ${item.animal_name}, scientific: ${item.scientific_name}`); + }); + return displayNames; } @@ -75,8 +82,12 @@ const zooAnimals = [ πŸ’‘ NOTE: Do some research for other methods that can help help you */ - function lowerCaseNames(/*Your Code Here*/){ - /*Your Code Here*/ + function lowerCaseNames(array){ + const lowerCase = []; + array.map(function(item){ + lowerCase.push(item.animal_name.toLowerCase()); + }); + return lowerCase; } @@ -88,8 +99,11 @@ const zooAnimals = [ 3. Return this new array */ - function lowPopulationAnimals(/*Your Code Here*/){ - /*Your Code Here*/ + function lowPopulationAnimals(array){ + array.filter(function(item){ + return item.population < 5; + }); + return array; } @@ -102,8 +116,11 @@ const zooAnimals = [ πŸ’‘ NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax! */ - function USApop(/*Your Code Here*/){ - /*Your Code Here*/ + function USApop(array){ + const sum = array.reduce(function(acc, item){ + return acc + item.population; + }, 0); + return sum; } @@ -116,9 +133,9 @@ const zooAnimals = [ πŸ’‘ NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2. */ - function consume(/*Your Code Here */){ - /*Your Code Here */ - } + function consume(a, b, cb){ + return cb(a, b); + } // 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁 @@ -128,9 +145,9 @@ const zooAnimals = [ 2. Return the sum of those numbers */ -function add(/*Your Code Here */){ - /*Your Code Here*/ - } + function add(a, b) { + return a + b; +} /* Use multiply to do the following: @@ -138,9 +155,9 @@ function add(/*Your Code Here */){ 2. Return the product of those numbers */ -function multiply(/*Your Code Here */){ - /*Your Code Here */ - } +function multiply(a, b) { + return a * b; +} /* Use greeting to do the following: @@ -149,9 +166,9 @@ function multiply(/*Your Code Here */){ πŸ’‘ NOTE: The string returned must match the format above or the test will not pass! */ -function greeting(/*Your Code Here */){ - return /*Your Code Here */ - } +function greeting(first_name, last_name) { + return `Hello ${first_name} ${last_name}, nice to meet you!`; +} // 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁 @@ -160,7 +177,9 @@ function greeting(/*Your Code Here */){ // 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! // 🐴🐴🐴 Topic 3: Prototypes 🐴🐴🐴 // @@ -175,8 +194,10 @@ function greeting(/*Your Code Here */){ - Instances of CuboidMaker should initialize `length`, `width` and `height` properties */ -function CuboidMaker(/*Your Code Here */){ - /*Your Code Here */ +function CuboidMaker(cuboid){ + this.length = cuboid.length; + this.width = cuboid.width; + this.height = cuboid.height } @@ -185,6 +206,9 @@ function CuboidMaker(/*Your Code Here */){ πŸ’‘ NOTE: Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume = function () { + return this.length * this.width * this.height +} @@ -193,14 +217,20 @@ function CuboidMaker(/*Your Code Here */){ πŸ’‘ NOTE: Formula for cuboid surface area: 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 (not auto graded)🐴🐴🐴 Create an object called cuboid 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({ + length: 4, + width: 5, + height: 5 + }) @@ -209,14 +239,32 @@ function CuboidMaker(/*Your Code Here */){ // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 +console.log(cuboid.volume()); // 100 +console.log(cuboid.surfaceArea()); // 130 // πŸ¦„πŸ¦„πŸ¦„ Topic 4: Classes πŸ¦„πŸ¦„πŸ¦„ // //Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class. class CuboidMakerTwo{ - + constructor (cuboidTwo) { + this.length = cuboidTwo.length; + this.width = cuboidTwo.width; + this.height = cuboidTwo.height + } + volume() { + return this.length * this.width * this.height + } + surfaceArea() { + return 2 * (this.length * this.width + this.length * this.height + this.width *this.height) + } } +const cuboidTwo = new CuboidMakerTwo({ + length: 4, + width: 5, + height: 5 +}) + @@ -224,7 +272,8 @@ class CuboidMakerTwo{ // console.log(cuboidTwo.volume()); // 100 // console.log(cuboidTwo.surfaceArea()); // 130 - +console.log(cuboidTwo.volume()); // 100 +console.log(cuboidTwo.surfaceArea()); // 130