From a001717ee462b2841e4add673eefc15fdbdf2e2c Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 19:13:14 -0600 Subject: [PATCH 01/10] Made consume function --- challenges/functions.js | 4 ++++ challenges/package-lock.json | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 challenges/package-lock.json diff --git a/challenges/functions.js b/challenges/functions.js index 6e3688bfcc..462bbe2156 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -6,8 +6,12 @@ * 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(firstVal, secondVal, CallBackVal){ + return CallBackVal(firstVal,secondVal); +} +console.log(consume('Robel','Mengistu')) /* 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 diff --git a/challenges/package-lock.json b/challenges/package-lock.json new file mode 100644 index 0000000000..48e341a095 --- /dev/null +++ b/challenges/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} From 7930b77a3f02976ad5b736d89262c05e44098576 Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 19:29:07 -0600 Subject: [PATCH 02/10] Made add multiply and greeting with two arguments each --- challenges/functions.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/challenges/functions.js b/challenges/functions.js index 462bbe2156..9d37d02989 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -7,22 +7,44 @@ * The consume function should return the invocation of cb, passing a and b into cb as arguments */ function consume(firstVal, secondVal, CallBackVal){ - return CallBackVal(firstVal,secondVal); - } -console.log(consume('Robel','Mengistu')) +//console.log(consume('Robel','Mengistu')) /* 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 gender(sVal){ +// if (sVal === "f" || sVal === "F") { +// return "Femal"; +// } else if (sVal === "m" || sVal === "M") { +// return "Male"; +// } else { +// return " Not a Gender"; +// } +// } + +//ADD +function add(valOne, valTwo){ + return valOne + valTwo; +} + +//MULTIPLY +function multiply(valOne, valTwo){ + return valOne * valTwo; +} + +//GREETINGS +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! + 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 ==== From e9487492378f27f2089bf3286490e5b6ebf1f655 Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 19:33:03 -0600 Subject: [PATCH 03/10] completed the functions by explaining closure --- challenges/functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenges/functions.js b/challenges/functions.js index 9d37d02989..eeaae38c46 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -51,7 +51,7 @@ function greeting(firstName, lastName){ // Explain in your own words why nestedfunction can access the variable internal. -// Explanation: +// Explanation: the inner funciton is able to access it due to closure,since internal variable is outside of the nestedfunction curly braces it can see it, const external = "I'm outside the function"; From de5ed5b68718f80b0bf5c13c8b1a20b60574b686 Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 19:41:31 -0600 Subject: [PATCH 04/10] Created the three objects --- challenges/objects-arrays.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index 86970b0c29..ff51996d97 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -7,10 +7,34 @@ */ // tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceous +const tyrannosaurus = { + name:'tyrannosaurus', + diet:'carnivorous', + weight:'7000kg', + length:'12m', + period:'Late Cretaceous' + +} // stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic +const stegosaurus = { + name:'stegosaurus', + diet:'herbivorous', + weight:'2000kg', + length:'9m', + period:'Late Jurassic' + +} // velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceous +const velociraptor = { + name:'velociraptor', + diet:'carnivorous', + weight:'15kg', + length:'1.8m', + period:'Late Cretaceous' + +} // Using your dinosaur objects, log answers to these questions: From 1eccef68249c598c479af9d218d2b28439f268af Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 19:46:09 -0600 Subject: [PATCH 05/10] made the roar method for MR tyrannosaurus --- challenges/objects-arrays.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index ff51996d97..60b33acba7 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -12,7 +12,11 @@ const tyrannosaurus = { diet:'carnivorous', weight:'7000kg', length:'12m', - period:'Late Cretaceous' + period:'Late Cretaceous', + roar = function(){ + return 'RAWERSRARARWERSARARARRRR!'; + } + } @@ -39,20 +43,20 @@ const velociraptor = { // 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 ==== From 1947cf7b9735ebc7837d9f0ae449886f8420eeda Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 21:09:20 -0600 Subject: [PATCH 06/10] made the uni search using includes --- challenges/objects-arrays.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index 60b33acba7..f885c292bf 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -77,9 +77,11 @@ const graduates = [ ]; /* Request 1: Create a new array called universities that contains all the universities in the graduates array. This will be an array of strings. - Once you have the new array created, log the result. */ const universities = []; +for (i = 0; i < graduates.length; i++){ + universities.push(graduates[i].university); +} 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. @@ -89,12 +91,23 @@ The resulting contact information strings should have a space between the first Log the result of your new array. */ const contactInfo = []; +for (i=0; i university === "Uni"); +// +for(i=0; i< graduates.length; i++){ + if(graduates[i].university.includes(searchVal) == true){ + unisWithUni.push(graduates[i].university); + } +} + console.log(unisWithUni); // ==== ADVANCED Array Methods ==== From a35ef458fce0691591df3a30e979d3538c4937ff Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 21:10:12 -0600 Subject: [PATCH 07/10] finshed making the totalpopulation tracker --- challenges/objects-arrays.js | 20 +++++++++++++++----- challenges/prototypes.js | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/challenges/objects-arrays.js b/challenges/objects-arrays.js index f885c292bf..396362d45b 100644 --- a/challenges/objects-arrays.js +++ b/challenges/objects-arrays.js @@ -101,7 +101,7 @@ const unisWithUni = []; let searchVal='Uni'; // const testRun = graduates.find() // const result = graduates.find(({ university }) => university === "Uni"); -// +//make sure to change searchVal if testing with direct console for(i=0; i< graduates.length; i++){ if(graduates[i].university.includes(searchVal) == true){ unisWithUni.push(graduates[i].university); @@ -109,6 +109,7 @@ for(i=0; i< graduates.length; i++){ } console.log(unisWithUni); + // ==== ADVANCED Array Methods ==== // Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems. @@ -132,7 +133,11 @@ The zoos want to display both the scientific name and the animal name in front o */ const displayNames = []; -console.log(displayNames); +zooAnimals.forEach(function(zooAnimals){ + displayNames.push(`${zooAnimals.scientific_name} ${zooAnimals.animal_name}`) +}) +console.log(displayNames) + /* Request 2: .map() @@ -140,7 +145,9 @@ The zoos need a list of all their animal's names (animal_name only) converted to */ -const lowCaseAnimalNames = []; +let lowCaseAnimalNames = zooAnimals.map((eachAnimal,index,zooAnimals)=>{ + return eachAnimal.animal_name.toLowerCase(); +}); console.log(lowCaseAnimalNames); /* Request 3: .filter() @@ -156,8 +163,11 @@ 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); +let totalPopulation = zooAnimals.reduce((population, eachAnimal,indexedDB,zooAnimals)=>{ + // return population = population + eachAnimal.population; + return population +=eachAnimal.population; + },0)//initial value issues + console.log(totalPopulation); /* diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 4cafc33e95..1bbf910bac 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -4,7 +4,9 @@ /* == Step 1: Base Constructor == Create a constructor function named CuboidMaker that accepts properties for length, width, and height + */ +function CuboidMaker /* == Step 2: Volume Method == From 0c43554fca4e213330bc2936ce239a68245876c3 Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 21:24:32 -0600 Subject: [PATCH 08/10] finished making the last subiodMaker obj --- challenges/prototypes.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 1bbf910bac..435f1e8623 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -6,7 +6,11 @@ Create a constructor function named CuboidMaker that accepts properties for length, width, and height */ -function CuboidMaker +function CuboidMaker(attributes){ + this.length = attributes.length; + this.width = attributes.width; + this.height = attributes.height; +} /* == Step 2: Volume Method == @@ -14,6 +18,9 @@ function CuboidMaker Formula for cuboid volume: length * width * height */ +CuboidMaker.prototype.volume=function(){ + return (this.length*this.width*this.height); +} /* == Step 3: Surface Area Method == @@ -21,15 +28,26 @@ function CuboidMaker Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ +CuboidMaker.prototype.surfaceAreaCube=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 cubidObj = 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 From cf7773a37924ab5e6c09a02c4fdb7f6e6a5b0b4b Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Sun, 3 May 2020 21:42:34 -0600 Subject: [PATCH 09/10] surface calculation completed on class --- challenges/classes.js | 20 ++++++++++++++++---- challenges/prototypes.js | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/challenges/classes.js b/challenges/classes.js index 992e39dc0b..f6c92866a9 100644 --- a/challenges/classes.js +++ b/challenges/classes.js @@ -1,7 +1,19 @@ // 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 this.length * this.width * this.height; + } + surfaceArea() { + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); + } +} // 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 +// 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. diff --git a/challenges/prototypes.js b/challenges/prototypes.js index 435f1e8623..d68fe6bb8b 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -28,8 +28,8 @@ CuboidMaker.prototype.volume=function(){ Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ -CuboidMaker.prototype.surfaceAreaCube=function(){ - return 2 * (this.length*this.width + this.length*this.height + this.width*this.height); +CuboidMaker.prototype.surfaceArea=function(){ + return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); }; From 0148958195ba2327cc3b555111b49bd62d5bbd30 Mon Sep 17 00:00:00 2001 From: Robel Mengistu <62121236+robelv2020@users.noreply.github.com> Date: Mon, 4 May 2020 20:41:58 -0600 Subject: [PATCH 10/10] fixed the new object name to match the console.log --- challenges/.vscode/launch.json | 17 +++++++++++++++++ challenges/functions.js | 2 +- challenges/prototypes.js | 8 ++++---- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 challenges/.vscode/launch.json diff --git a/challenges/.vscode/launch.json b/challenges/.vscode/launch.json new file mode 100644 index 0000000000..5594169e78 --- /dev/null +++ b/challenges/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\functions.js" + } + ] +} \ No newline at end of file diff --git a/challenges/functions.js b/challenges/functions.js index eeaae38c46..85f30fb46c 100644 --- a/challenges/functions.js +++ b/challenges/functions.js @@ -51,7 +51,7 @@ function greeting(firstName, lastName){ // Explain in your own words why nestedfunction can access the variable internal. -// Explanation: the inner funciton is able to access it due to closure,since internal variable is outside of the nestedfunction curly braces it can see it, +// Explanation: the inner funciton is able to access it due to rule of closure,since internal variable is outside of the nestedfunction curly braces it can see it, const external = "I'm outside the function"; diff --git a/challenges/prototypes.js b/challenges/prototypes.js index d68fe6bb8b..056a7f392b 100644 --- a/challenges/prototypes.js +++ b/challenges/prototypes.js @@ -18,7 +18,7 @@ function CuboidMaker(attributes){ Formula for cuboid volume: length * width * height */ -CuboidMaker.prototype.volume=function(){ +CuboidMaker.prototype.volume = function(){ return (this.length*this.width*this.height); } @@ -28,7 +28,7 @@ CuboidMaker.prototype.volume=function(){ Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height) */ -CuboidMaker.prototype.surfaceArea=function(){ +CuboidMaker.prototype.surfaceArea = function(){ return 2 * (this.length * this.width + this.length * this.height + this.width * this.height); }; @@ -47,7 +47,7 @@ const cubidObj = new CuboidMaker({ }); // Test your volume and surfaceArea methods by uncommenting the logs below: -console.log(cuboid.volume()); // 100 -console.log(cuboid.surfaceArea()); // 130 +console.log(cubidObj.volume()); // 100 +console.log(cubidObj.surfaceArea()); // 130