diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..6f3a2913e1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/README.md b/README.md index cc6d6902fb..d944441c13 100644 --- a/README.md +++ b/README.md @@ -26,27 +26,40 @@ Edit this document to include your answers after each question. Make sure to lea 1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max) +.forEach is a method that executes a function once for each array element, and .map is another method that creates a new array with the result of calling a provided funciton on every element in the calling array. One of the different between them is that .forEach doesnt return anything, but .map always return a new array of the same size. .forEach will change our original array, and .map will keep the same array, returning a new array. One of the similarity of both methods is that they will call a provided function on every element of the array, and also because they are both array iterations. + 2. Explain the difference between a callback and a higher order function. +A higher order function is the main function that can have a function inside it, and use it as an argument. and a callback function is the child function of the main function(this case the higher order function), expecting it to call it. + 3. What is closure? +closure is the combination of a function, working together with its lexical environment(its surrounding). A closure can give us access to an outsider function scope from a inner function. also, if there is not a variable defined inside the function scope, it will look outside the scope, searching for a variable being referenced outside the function scope. + 4. Describe the four rules of the 'this' keyword. +1. Global binding: its used when a function is contained in the globa scope, and this value inside the function will be the window object. +2. Implicit binding: its used when a function is called by a precedent dot, and the object before that dot is this. +3. New binding: its used when it creates a new object based on a constructor function, and later, it passing in new arguments. +4. Explicity Binding: its used when we use .call, and .apply to create new objects from previous objects. + 5. Why do we need super() in an extended class? +we use super() in an extended class because by using this method, we will call the parent's constructor method, and will get access to the parent's propierties and methods. + ### Task 1 - Project Set up Follow these steps to set up and work on your project: Make sure you clone the branch that the TK links to: the vnext branch, NOT master! -- [ ] Create a forked copy of this project. -- [ ] Add TL as collaborator on Github. -- [ ] Clone your OWN version of Repo (Not Lambda's by mistake!). -- [ ] Create a new Branch on the clone: git checkout -b ``. -- [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project. -- [ ] You are now ready to build this project with your preferred IDE -- [ ] Implement the project on your Branch, committing changes regularly. -- [ ] Push commits: git push origin ``. +- [x ] Create a forked copy of this project. +- [x ] Add TL as collaborator on Github. +- [x ] Clone your OWN version of Repo (Not Lambda's by mistake!). +- [ x] Create a new Branch on the clone: git checkout -b ``. +- [ x] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project. +- [x ] You are now ready to build this project with your preferred IDE +- [ x] Implement the project on your Branch, committing changes regularly. +- [x ] Push commits: git push origin ``. @@ -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. @@ -86,6 +99,6 @@ There are a few stretch problems found throughout the files, don't work on them Follow these steps for completing your project: -- [ ] Submit a Pull-Request to merge Branch into master (student's Repo). -- [ ] Add your team lead as a Reviewer on the Pull-request -- [ ] TL then will count the HW as done by merging the branch back into master. +- [x ] Submit a Pull-Request to merge Branch into master (student's Repo). +- [ x] Add your team lead as a Reviewer on the Pull-request +- [ x] TL then will count the HW as done by merging the branch back into master. diff --git a/challenges/arrays-callbacks.js b/challenges/arrays-callbacks.js index 472ab3e96d..4939f934cb 100644 --- a/challenges/arrays-callbacks.js +++ b/challenges/arrays-callbacks.js @@ -21,6 +21,13 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +// zooAnimals.forEach( function(array) { +// return displayNames.push(`Name: ${array.animal_name}, Scientific: ${array.scientific_name}`); +// }); +//stretch with arrow functions + +zooAnimals.forEach((array) => displayNames.push(`Name: ${array.animal_name}, Scientific: ${array.scientific_name}`)); + console.log(displayNames); /* Request 2: .map() @@ -30,6 +37,13 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +// zooAnimals.map( function(array) { +// return lowCaseAnimalNames.push(array.animal_name.toLowerCase()); +// }); +//stretch with arrow functions + +zooAnimals.map((array) => lowCaseAnimalNames.push(array.animal_name.toLowerCase())); + console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -37,7 +51,13 @@ console.log(lowCaseAnimalNames); 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( function(array) { +// return array.population < 5; +// }); +//stretch with arrow functions + +const lowPopulationAnimals = zooAnimals.filter((array) => array.population < 5); + console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -45,7 +65,13 @@ 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( function(total, array) { +// return total + array.population +// },0); +//stretch with arrow functions + +const populationTotal = zooAnimals.reduce((total, array) => total + array.population,0); + console.log(populationTotal); @@ -57,7 +83,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) { + 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 @@ -65,13 +93,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!" */ +function add(sum1, sum2) { + return sum1 + sum2; +}; -/* 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! - +function multiply(num1, num2) { + return num1 * num2; +}; +function greeting(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..a078d3c298 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,75 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. -// Test your volume and surfaceArea methods by uncommenting the logs below: -// console.log(cuboid.volume()); // 100 -// console.log(cuboid.surfaceArea()); // 130 +/* == Step 1: Base Constructor == + Create a constructor function named CuboidMaker that accepts properties for length, width, and height +*/ + class CuboidMaker { + constructor (attrs) { + this.length = attrs.length; + this.width = attrs.width; + this.height = attrs.height; + }; -// 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 + + /* == Step 2: Volume Method == + Create a method using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height + + Formula for cuboid volume: length * width * height + */ + + volume() { + 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) + */ + + surfaceArea() { + 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({ + 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 + +// 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 CuboidMaker { + constructor(cubeAttrs) { + super(cubeAttrs); + }; + //formula to find the volume of a cube is v = length * width * height + volume() { + return this.length * this.width * this.height; + }; + + // formula to fnd surface area of a cube is sa: 2 * (length * width + length * height + width * height) + surfaceArea() { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height) + }; + }; + +const cube = new CubeMaker ({ + length: 4, + width: 4, + height: 4 +}); + +console.log(cube.volume()); //64 +console.log(cube.surfaceArea()); //96 \ No newline at end of file diff --git a/challenges/closure.js b/challenges/closure.js index 101d68e553..a7a2e84e45 100644 --- a/challenges/closure.js +++ b/challenges/closure.js @@ -16,9 +16,22 @@ function myFunction() { } myFunction(); -// Explanation: +// Explanation: nested function can access the variable internal because they both are inside the same function myFunction scope. Also, it is a closure because nestedFunction is using a variable that is outside its function byt aroung its lexical environment. /* 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 summation(num) { + let sum = 0; + return function() { + for(let i=1; i <= num; i++) { + sum += i; + } + return sum; + } +}; + +const sumTotal = summation(4); +console.log(sumTotal()); \ No newline at end of file diff --git a/challenges/index.html b/challenges/index.html index 5772804b7f..0dc5ec27dd 100644 --- a/challenges/index.html +++ b/challenges/index.html @@ -7,8 +7,8 @@ Sprint Challenge - - + + diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..1bc988c482 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 */ - +function CuboidMaker(attrs) { + this.length = attrs.length; + this.width = attrs.width; + this.height = attrs.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,10 @@ 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 +28,24 @@ 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({ + 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