Skip to content
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

arrays done #1244

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ Demonstrate your understanding of this week's concepts by answering the followin
Edit this document to include your answers after each question. Make sure to leave a blank line above and below your answer so it is clear and easy to read by your team lead

1. Briefly compare and contrast `.forEach` & `.map` (2-3 sentences max)

.forEach does not put a new array into memory, .map does
2. Explain the difference between a callback and a higher order function.

a callback invokes a higher-order function
3. What is closure?

a function and what it can remember
4. Describe the four rules of the 'this' keyword.

in the global scope refers to the window object.
in implicit binding- when caled in a function represents the object preceding the dot
in explicit binding-refers to the .call or .apply method
in new binding refers to the specific instance in a constructor
5. Why do we need super() in an extended class?

it replaes the .call and .apply methods
### Task 1 - Project Set up

Follow these steps to set up and work on your project:
Expand Down
37 changes: 29 additions & 8 deletions challenges/arrays-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const zooAnimals = [
The zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus."

*/
const displayNames = [];
const displayNames = []
zooAnimals.forEach((name) => {
displayNames.push(`animal_name: ${name.animal_name}, scientific_name: ${name.scientific_name}.`);
})
console.log(displayNames);

/* Request 2: .map()
Expand All @@ -30,22 +33,28 @@ The zoos need a list of all their animal's names (animal_name only) converted to
*/

const lowCaseAnimalNames = [];
zooAnimals.map((name) => {
lowCaseAnimalNames.push( name.animal_name.toLowerCase());
});
console.log(lowCaseAnimalNames);

/* Request 3: .filter()

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((data) => {
return data.population > 6
});
console.log(lowPopulationAnimals);

/* 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. 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((total,animal) => total + animal.population,0);

console.log(populationTotal);


Expand All @@ -57,19 +66,31 @@ 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
* 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(cb1,cb2){
return cb1 + cb2;
};
function multiply(cb1,cb2){
return cb1*cb2;
};
function greeting(cb1,cb2){
return `Hello ${cb1.toLowerCase()} ${cb2.toUpperCase()}, 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!



Expand Down
31 changes: 29 additions & 2 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.
class CircleMaker {
constructor(diameter) {
this.diameter = diameter;
}
area() {
return Math.PI * Math.pow((this.diameter / 2), 2);
}
circumference() {
return 2 * Math.PI * (this.diameter / 2);
}
}


const circle = new CircleMaker(5);

// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
class SphereMaker extends CircleMaker {
constructor(diameter) {
super(diameter);
}
volume() {
return 4 / 3 * Math.PI * Math.pow(this.diameter / 2, 3);
}
area() {
return 4 * Math.PI * Math.pow(this.diameter / 2, 2);
}
}

const sphere = new SphereMaker(5);
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.
14 changes: 13 additions & 1 deletion challenges/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ function myFunction() {
}
myFunction();

// Explanation:
// Explanation: Because a funcction that is nested within can see out but another function cannot see in.


/* 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 sumation(num){
let counter = 1;
return function() {
for(num; num > 1; num--) {
counter = counter + num
}
return counter;
}
};
const addition = sumation(4);

console.log(addition());
21 changes: 13 additions & 8 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,35 @@

/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/
*/function CuboidMaker(length,width,height){
this.length = length;
this.width = width;
this.height = 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(4,5,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());