diff --git a/README.md b/README.md index 408612117c..89dfad0ee9 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,35 @@ Edit this document to include your answers after each question. Make sure to lea 1. Describe the biggest difference between `.forEach` & `.map`. + .map() creates a brand new array to hold the values and .forEach() does not. + + 2. What is the difference between a function and a method? +• Function is a set of code to do some task in JavaScript. +• Method is also a function which is used as a property in Object. + 3. What is closure? +• a closure gives you access to an outer function’s scope from an inner function. + + 4. Describe the four rules of the 'this' keyword. +• Implicitly bound items from an object within that object's methods. Implicit Binding- you look left of the dot(.) at call time. + +• Explicitly bound items which are in an object using a function outside of that object. Explicit Binding- telling a function what the context of the keyword is going to be using call() apply() bind () + +• The "new" keyword in order to make new objects. New Binding- whenever you have a function that invoke new keyword .this keyword is bound to new object being constructed. + +• Window Binding - when none of the above rules apply, then .this keyword is going to default to window object unless your in strict mode, then its just going to be undefined. + + 5. Why do we need super() in an extended class? + +• the Super allows the code to access the parent class's properties and methods. + ## Project Set up Follow these steps to set up and work on your project: diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..b539445066 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,28 @@ // 1. Copy and paste your prototype in here and refactor into class syntax. +class CuboidMaker{ + constructor(attributes){ + this.length = attributes.length + this.width = attributes.width + this.height = attributes.height + } + volume() { + return `Cuboid volume: ${(this.length * this.width * this.height)}` + } + surfaceArea() { + return `Surface Area = ${2 *(this.length * this.width + this.length * this.height + this.width * this.height)}`; + } +}; + + +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 // 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/functions.js b/challenges/functions.js index 6e3688bfcc..e48efb480f 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -6,6 +6,10 @@ * The last parameter accepts a callback * The consume function should return the invocation of cb, passing a and b into cb as arguments */ +let consume =function(a, b, cb){ + return cb(a,b) +} + /* Step 2: Create several functions to callback with consume(); @@ -14,11 +18,21 @@ * Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!" */ +let add = (num1, num2) => { + return num1 + num2; +} +let multiply = (num1, num2) => { + return num1 * num2; +} +let 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(): */ // 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(10, 16, multiply)); // 160 +console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! // ==== Closures ==== diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index 659e3e149c..a261d43ee8 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -8,27 +8,54 @@ // tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous +let tyrannosaurus = { + name: 'tyrannosaurus', + diet: 'carnivorous', + weight: '7000kg', + length: '12m', + period: 'Late Cretaceous', +} + // stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic +let stegosaurus = { + name: 'stegosaurus', + diet: 'herbivorous', + weight: '2000kg', + length: '9m', + period: 'Late Jurassic' +} + // velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceous +let velociraptor = { + name: 'velociraptor', + diet: 'carnivorous', + weight: '15kg', + length: '1.8m', + period: 'Late Cretaceous', + rawr: function (){ + return "RAWERSRARARWERSARARARRRR!" + } +} + // Using your dinosaur objects, log answers to these questions: // How much did tyrannosaurus weigh? -console.log(); +console.log(tyrannosaurus.weight); // What was the diet of a velociraptor? -console.log(); +console.log(velociraptor.diet); // How long was a stegosaurus? -console.log(); +console.log(stegosaurus.length); // What time period did tyrannosaurus live in? -console.log(); +console.log(tyrannosaurus.period); // Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result. -console.log(); +console.log(tyrannosaurus.roar()); // ==== Arrays ==== @@ -52,6 +79,10 @@ const graduates = [ Once you have the new array created, sort the universities alphabetically and log the result. */ const universities = []; +for(let i = 0; i < graduates.length; i++) { + universities.push(graduates[i].university); + universities.sort(); +} console.log(universities); /* Request 2: Create a new array called contactInfo that contains both first name and email of each student. This will be an array of strings. @@ -61,10 +92,22 @@ The resulting contact information strings should have a space between the first Log the result of your new array. */ const contactInfo = []; + +for(let i = 0; i < graduates.length; i++){ + contactInfo.push(`${graduates[i].first_name} ${graduates[i].email}`); +} + console.log(contactInfo); /* Request 3: Find out how many universities have the string "Uni" included in their name. Create a new array called unisWithUni that contains them all. This will be an array of objects. Log the result. */ const unisWithUni = []; + +for (i=0 ;i