diff --git a/README.md b/README.md index cc6d6902fb..9da4ad757c 100644 --- a/README.md +++ b/README.md @@ -25,15 +25,28 @@ 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 by your team lead 1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) +forEach gives once for each single array element, while map creates a new array calling the items in the array. +forEach doesnt actually return anything while map does. 2. Explain the difference between a callback and a higher order function. +higher order function is a function that can recieve other functions, it operates with other functions. +a callback will be passed to another function and called after the function is done executing 3. What is closure? +function that is nested into another function and has access to the block scope of the other function + 4. Describe the four rules of the 'this' keyword. +1)Binds to the global scope +2)Implicit binding; binds to the object this.length (.) object +3) new biding object (.) length +4) explicit binding with new functions .call and .apply + 5. Why do we need super() in an extended class? +the super() allows the parent item to access the child element. it notfys the attention of the parent element + ### Task 1 - Project Set up Follow these steps to set up and work on your project: @@ -59,22 +72,22 @@ Your finished project must include all of the following requirements: #### Task A: Objects and Arrays Test your knowledge of advanced array methods and callbacks. -* [ ] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully! +* [x ] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully! #### Task B: Closure This challenge takes a look at closures as well as scope. -* [ ] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully! +* [x ] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully! #### Task C: Prototypes Create constructors, bind methods, and create cuboids in this prototypes challenge. -* [ ] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully! +* [x ] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully! #### Task D: Classes Once you have completed the prototypes challenge, it's time to convert all your hard work into classes. -* [ ] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully! +* [ x] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully! In your solutions, it is essential that you follow best practices and produce clean and professional results. Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work. It is better to submit a challenge that meets MVP than one that attempts too much and does not. diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..a16c392a88 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,23 +21,33 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +zooAnimals.forEach(element => { + return displayNames.push(`Name : ${element.animal_name} , Scientific Name: ${element.scientific_name}`) +}); 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. */ -const lowCaseAnimalNames = []; +const lowCaseAnimalNames = zooAnimals.map(element => { + return element.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. */ -const lowPopulationAnimals = []; +const lowPopulationAnimals = zooAnimals.filter(element => { + return element.population <= 5; +}) console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,9 +55,10 @@ 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; -console.log(populationTotal); - +const populationTotal = zooAnimals.reduce(function (arr1,arr2){ + return arr1 + arr2.population; + },0) + console.log(populationTotal); // ==== Callbacks ==== @@ -57,7 +68,9 @@ 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 */ - +function consume(a,b,cb){ + console.log(cb(a,b)); + } /* Step 2: Create several functions to callback with consume(); * Create a function named add that returns the sum of two numbers @@ -65,7 +78,17 @@ 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!" */ +function add(x,y){ + returns (x + y); +} + +function multiply(x,y){ + returns (x*y); +} +function greeting(firstName,lastName){ + returns `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 diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..987aea66ab 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -3,5 +3,28 @@ // Test your volume and surfaceArea methods by uncommenting the logs below: // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130 +class CuboidMaker { + constructor(atts) { + this.length = atts.length; + this.width = atts.width; + this.height = atts.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 cuboid = new CuboidMaker ({ + length: 4, + width: 5, + height: 5 + }); + + + + // 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..50afad2601 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -18,7 +18,18 @@ myFunction(); // Explanation: +// nestfunctions can acess the variable internal because nested functions, although is a new function, +//its still in the block scope of myFunction. +//a function that wasnt in the same block scope couldnt access that variable. /* 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. */ +/* 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(n) { + return n * (n + 1) / 2; +} + +console.log( sumation(4) ); \ No newline at end of file diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..da960168af 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -1,3 +1,6 @@ + + +// Test your volume and surfaceArea methods by uncommenting the logs below: /* ===== 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. @@ -5,6 +8,11 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ +function CuboidMaker (atts) { + this.length = atts.length; + this.width = atts.width; + this.height = atts.height; +}; /* == Step 2: Volume Method == @@ -13,12 +21,19 @@ 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. 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 == @@ -26,8 +41,17 @@ Add properties and values of length: 4, width: 5, and height: 5 to cuboid. */ +const 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 + + +