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 408612117c..e70f86f869 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,29 @@ Edit this document to include your answers after each question. Make sure to lea 1. Describe the biggest difference between `.forEach` & `.map`. +ANSWER : forEach just calls a function and doesn't return a new array, map does return a new array + 2. What is the difference between a function and a method? +ANSWER : functions can be used on anything, methods are used on objects. + 3. What is closure? +ANSWER : variables that can be accessed through inheritance, giving an inner function access to an outer functions variables. + 4. Describe the four rules of the 'this' keyword. + ANSWER: Window binding : 'this' goes to the window/console object + + Implicit binding : when a function references the object to the left of the dot (.) + + New binding : used in conjunction with a constructor function, using the 'new' keyword will make am enitrely new object with the values inside the parentheses. + + Explicit binding : use call or apply to change what 'this' points to 5. Why do we need super() in an extended class? +ANSWER: When using Classes they link the child object to the parent object, which dictates what the child will inherit + ## 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..e74264e2f8 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,74 @@ -// 1. Copy and paste your prototype in here and refactor into class syntax. +// 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 = function() { + return this.length * this.width * this.height; + } + surfaceArea = function() { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); + } + + } + + class CubeMaker extends CuboidMaker { + constructor(cubeAttributes){ + super(cubeAttributes); + } + volume() { + return Math.pow(this.length, 3); + } + surfaceArea() { + return 6 * Math.pow(this.length, 2); + } + } + + /* == 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 + */ +// 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 == + 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 + }); + + const cube = new CubeMaker({ + length: 4, + width:4, + height: 4 + }); // 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 + + +console.log(cube.volume()); +console.log(cube.surfaceArea()); // 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..a08d29505c 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -7,18 +7,29 @@ * 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 * 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!" */ - +function add(num1, num2) { + return num1 + num2; +} +function multiply(num1, num2) { + return num1 * num2; +} +function greeting(a, b) { + return `Hello ${a} ${b}, 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(2, 2, add)); // 4 +console.log(consume(10, 16, multiply)); // 160 +console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you! // ==== Closures ==== @@ -27,6 +38,8 @@ // Explanation: +// ANSWER : Because of Closure it inherited that variable from its outer function, the outer function (myFunction) works like a parent to the (nestedFunction) child // + const external = "I'm outside the function"; diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index 659e3e149c..54adcf562a 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -5,6 +5,33 @@ Use this pattern to create your objects: object name, diet, weight, length, period */ +const obj1 = { + name: 'tyrannosaurus', + diet: 'carnivorous', + weight: '7000kg', + length: '12m', + period: 'Late Cretaceous', + Roar(){ + return "RAWERSRARARWERSARARARRRR!"; + + } +} + +const obj2 = { + name: 'stegosaurus', + diet: 'herbivorous', + weight: '2000kg', + length: '9m', + period: 'Late Jurassic' +} + +const obj3 = { + name: 'velociraptor', + diet: 'carnivorous', + weight: '15kg', + length: '1.8m', + period: 'Late Cretaceous' +} // tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous @@ -15,20 +42,20 @@ // Using your dinosaur objects, log answers to these questions: // How much did tyrannosaurus weigh? -console.log(); +console.log(obj1.weight); // What was the diet of a velociraptor? -console.log(); +console.log(obj3.diet); // How long was a stegosaurus? -console.log(); +console.log(obj2.length); // What time period did tyrannosaurus live in? -console.log(); +console.log(obj1.period); // Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result. -console.log(); +console.log(obj1.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 = []; +graduates.forEach((items) => { + return universities.push(`${items.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,20 @@ The resulting contact information strings should have a space between the first Log the result of your new array. */ const contactInfo = []; +graduates.forEach((items) => { + return contactInfo.push(`${items.first_name} ${items.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 = []; +const unisWithUni = graduates.map(items => items["university"]).filter(items => items.includes("Uni")); + +// for (let i = 0; i < graduates.length; i++){ +// if (graduates[i].university === "Uni") { +// unisWithUni.push(graduates[i]) +// } +// }; + console.log(unisWithUni); @@ -91,6 +132,10 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; +zooAnimals.forEach((items) => { + return displayNames.push(`${items.animal_name}, ${items.scientific_name}`); + +}); console.log(displayNames); /* Request 2: .map() @@ -100,6 +145,9 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ const lowCaseAnimalNames = []; +zooAnimals.map((items) => { + return lowCaseAnimalNames.push(`${items.animal_name.toLowerCase()}`); +}); console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -107,7 +155,8 @@ 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(items => items.population < 5); + console.log(lowPopulationAnimals); /* Request 4: .reduce() @@ -115,7 +164,9 @@ 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((accumulator, currentValue) =>{ + return accumulator + currentValue.population; +}, 0); console.log(populationTotal); diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..8b030fc15b 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,27 +6,43 @@ Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ +// function CuboidMaker(attributes) { +// this.length = attributes.length, +// this.width = attributes.width, +// this.height = attributes.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 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 == 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: // -// Test your volume and surfaceArea methods by uncommenting the logs below: // console.log(cuboid.volume()); // 100 // console.log(cuboid.surfaceArea()); // 130