Repository navigation
Amir yunas #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Amir yunas #1
Changes from all commits
650aebc
b4d0267
b050755
8899b37
4f7dfbf
d020717
0e90918
98e1511
3c292c7
886bbea
2a525f2
673c9ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,50 @@ | |
| // 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. | ||
| // 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. | ||
|
|
||
|
|
||
| // 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. | ||
|
|
||
| /* == Step 1: Base Constructor == | ||
| Create a constructor function named CuboidMaker that accepts properties for length, width, and height | ||
| */ | ||
|
|
||
| class CuboidMaker { | ||
| constructor(keys) { | ||
| this.name = keys.name; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome that you added an additional property here |
||
| this.length = keys.length; | ||
| this.width = keys.width; | ||
| this.height = keys.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 | ||
| */ | ||
| volume() { | ||
| return `The volume of the ${this.name} is ${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 `The Surface Area of the ${this.name} is ${2 * (this.length * this.width + this.length * this.height + this.width * this.height)}`; | ||
| } | ||
| } | ||
|
|
||
| const cube = new CuboidMaker({name: 'cube', length: 5, width: 3, height: 10}); | ||
| console.log(cube.surfaceArea()); | ||
|
|
||
| /* == 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({name: 'cuboid', 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,10 @@ | |
| * In the body of the function return the callback with the two parameters that you created | ||
| */ | ||
|
|
||
| function consume(arg1,arg2,cb) { | ||
| return cb(arg1,arg2); | ||
| } | ||
|
|
||
|
|
||
| /* Step 2: Create several functions to callback with consume(); | ||
| * Create a function named add that returns the sum of two numbers | ||
|
|
@@ -15,17 +19,32 @@ | |
| */ | ||
|
|
||
|
|
||
| function add(num1,num2) { | ||
| return num1+num2; | ||
| } | ||
|
|
||
| function multiply(num1,num2) { | ||
| return num1*num2; | ||
| } | ||
|
|
||
| function greeting(first,last) { | ||
| return `Hello ${first} ${last}, nice to meet you!`; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /* Step 3: Check your work by un-commenting the following calls to consume(): */ | ||
| // consume(2,2,add); // 4 | ||
| // consume(10,16,multiply); // 160 | ||
| // consume("Mary","Poppins", greeting); // Hello Mary Poppins, nice to meet you! | ||
| console.log(`\nThe result of calling the consume function with the add callback function: ${consume(2,2,add)}\n`); // 4 | ||
| console.log(`The result of calling the consume function with the multiply callback function: ${consume(10,16,multiply)}\n`); | ||
| console.log(`The result of calling the consume function with the greeting callback function: ${consume("Mary","Poppins", greeting)}\n`); | ||
|
|
||
|
|
||
|
|
||
| // ==== Closures ==== | ||
|
|
||
| // Explain in your own words why `nestedfunction()` can access the variable `internal`. | ||
|
|
||
| // Explanation: | ||
| console.log(`Explanation: nestedFunction() is able to access the variable internal, even though it is outside of the function scope. This is because of the concept in javascript known as a closure. Javascript allows for this behavior in order to allow functions to behave correctly. Technically, a function cannot access variables outside of it's scope. But because of the closure, a nested function can access variables in functions outside of it, and even in the global context.\n`); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an excellent answer! |
||
|
|
||
|
|
||
| const external = "I'm outside the function"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,30 +6,56 @@ | |
| object name, diet, weight, length, period | ||
| */ | ||
|
|
||
|
|
||
| // tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceious | ||
| const tyrannosaurus = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent work creating objects! |
||
| name: 'tyrannosaurus', | ||
| diet: 'carnivorous', | ||
| weight: '7000kg', | ||
| length: '12m', | ||
| period: 'Late Cretaceious', | ||
| roar: function() {return 'RAWERSRARARWERSARARARRRR!'} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome work adding in the roar method! |
||
| }; | ||
| // Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result. | ||
|
|
||
|
|
||
|
|
||
| // 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 Cretaceious | ||
| const velociraptor = { | ||
| name: 'velociraptor', | ||
| diet: 'carnivorous', | ||
| weight: '15kg', | ||
| length: '1.8m', | ||
| period: 'Late Cretaceious' | ||
| }; | ||
|
|
||
| // 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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure to add the roar method! |
||
| console.log(); | ||
|
|
||
| console.log(tyrannosaurus.roar()); | ||
|
|
||
| // ==== Arrays ==== | ||
|
|
||
|
|
@@ -50,7 +76,12 @@ const graduates = [{"id":1,"first_name":"Cynde","university":"Missouri Southern | |
|
|
||
| Once you have the new array created, sort the universities alphabetically and log the result. */ | ||
| const universities = []; | ||
| console.log(universities) | ||
|
|
||
| for (let i = 0; i < graduates.length; i++) { | ||
| universities.push(graduates[i].university); | ||
| } | ||
|
|
||
| console.log(universities.sort()); | ||
|
|
||
| /* Request 2: Create a new array called contactInfo that contains both first name and email of each student. | ||
|
|
||
|
|
@@ -59,13 +90,27 @@ Name email@example.com | |
|
|
||
| Log the result of your new array. */ | ||
| const contactInfo = []; | ||
|
|
||
| console.log('\n'); | ||
| 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 uni that contains them all. Log the result. */ | ||
| const uni = []; | ||
| console.log(uni); | ||
|
|
||
| console.log('\n'); | ||
| for (let i = 0; i < graduates.length; i++){ | ||
| if(graduates[i].university.includes('Uni')) { | ||
| uni.push(graduates[i].university); | ||
| } | ||
| } | ||
|
|
||
| console.log(uni); | ||
| console.log('\n'); | ||
|
|
||
| // ==== ADVANCED Array Methods ==== | ||
|
|
||
|
|
@@ -83,12 +128,16 @@ zooAnimals = [{"animal_name":"Jackal, asiatic","population":5,"scientific_name": | |
| {"animal_name":"Hawk-eagle, crowned","population":10,"scientific_name":"Spizaetus coronatus","state":"Florida"}, | ||
| {"animal_name":"Australian pelican","population":5,"scientific_name":"Pelecanus conspicillatus","state":"West Virginia"}]; | ||
|
|
||
|
|
||
| /* Request 1: .forEach() | ||
|
|
||
| The zoo wants to display both the scientific name and the animal name in front of the habitats. Return an array with only the animal and scientific names in it. The individual values in the array should look like this "Name: Jackal, asiatic, Scientific: Canis aureus." | ||
|
|
||
| */ | ||
| const animalNames = []; | ||
|
|
||
| zooAnimals.forEach( (animal,i) => animalNames.push('Name: ' + zooAnimals[i].animal_name,'Scientific: ' + zooAnimals[i].scientific_name)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome that you are using arrow syntax! |
||
|
|
||
| console.log(animalNames); | ||
|
|
||
| /* Request 2: .map() | ||
|
|
@@ -97,29 +146,43 @@ The zoos need a list of all their animal's names (names only, not scientific) co | |
|
|
||
| */ | ||
|
|
||
| const lowerCase = []; | ||
| //zooAnimals.forEach( (animal,i) => lowerCase.push(zooAnimals[i].animal_name.toLowerCase())); | ||
|
|
||
| const lowerCase = zooAnimals.map( (animal,i) => animal.animal_name.toLowerCase() ); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking good! It doesn't look like you're using the second parameter in your callback function, so you could remove it |
||
|
|
||
| console.log('\n The zoo animals\' names all lowercased: '); | ||
| console.log(lowerCase); | ||
|
|
||
| /* Request 3: .filter() | ||
|
|
||
| The zoos are concenred about animals with a lower population count. Find out which animals have a population less than 5. | ||
|
|
||
| */ | ||
| const largerPopulation = []; | ||
| console.log(largerPopulation); | ||
|
|
||
| const smallerPopulation = zooAnimals.filter((animal,i) => zooAnimals[i].population < 5).map( (animal,i) => (`${animal.animal_name}, Population: ${animal.population}`)); | ||
|
|
||
| //const smallerNames = smallerPopulation.map( (animal,i) => (`${smallerPopulation[i].animal_name}, Population: ${smallerPopulation[i].population}`)); | ||
|
|
||
| console.log('\n An array of the zoo animals that have a population less than 5: \n'); | ||
| console.log(smallerPopulation); | ||
|
|
||
|
|
||
|
|
||
| /* Request 4: .reduce() | ||
|
|
||
| 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. | ||
|
|
||
| */ | ||
| const populationTotal = 0; | ||
| console.log(populationTotal); | ||
| let populationTotal = 0; | ||
|
|
||
| populationTotal = zooAnimals.reduce( (total,animal) => total + animal.population, 0 ); | ||
|
|
||
| console.log('\n The total population for all animals in the zoo is: ' + populationTotal + '\n'); | ||
|
|
||
|
|
||
| /* | ||
|
|
||
| Stretch: If you haven't already, convert your array method callbacks into arrow functions. | ||
|
|
||
| //all callback functions within the array methods already use arrow functions. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent answers!