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

Daniel Beggs #1253

Open
wants to merge 4 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
23 changes: 23 additions & 0 deletions challenges/answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
1. Briefly compare and contrast .forEach & .map (2-3 sentences max)

The .forEach method calls a function once for each array element. Contrast this with the .map method which creates a new array with the same elements of the array you are calling in the method. In other words, .map will copy the elements of an array over to a new array.

2. Explain the difference between a callback and a higher order function.

The difference between a callback and a higher order function is the way that the function is passed through the anonymous function in your code. For example a callback function is a function passed into another function as an argument, which is called inside the outer function to do a job. While a higher order function has a parent child relationship with the functions they are trying to call.

3. What is closure?

Closure in javascript is how to define what scope a variable or function will be called in. These scopes include local scope, it has access to the outer function, and global variables. So this is analogous to the nesting in less CSS with the closures dictating the parent child relationship with the

4. Describe the four rules of the 'this' keyword.

-Whenever a function is contained in the global scope, the value of this inside of that function will be the window object.
-Whenever a function is called by a preceding dot, the object before that dot is this.
-Whenever a constructor function is used, this refers to the specific instance of the object that is created and returned by the constructor function.
-Whenever JavaScript’s call or apply method is used, this is explicitly defined.


5. Why do we need super() in an extended class?

Super() is used to call the functions of a parent of a higher order object.
78 changes: 49 additions & 29 deletions challenges/arrays-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
// 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.

const zooAnimals = [
{ animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" },
{ animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" },
{ animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" },
{ animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" },
{ animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" },
{ animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" },
{ animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" },
{ animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" },
{ 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" },
{ animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" },
{ animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" },
{ animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" },
{ animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" },
{ animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" },
{ animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" },
{ animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" },
{ animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" },
{ 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 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 = [];
let displayNames = [];
displayNames = zooAnimals.forEach(animal => animal.population.state.slice());
console.log(displayNames);

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

*/

const lowCaseAnimalNames = [];
console.log(lowCaseAnimalNames);
let lowerCase = [];
lowerCase = zooAnimals.map(animal => animal.animal_name.toLowerCase());
console.log(lowerCase);

/* 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 = [];
let lowPopulationAnimals = [];
lowPopulationAnimals = zooAnimals.filter(animal => animal.population.length > 5)
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;

let populationTotal = [];
populationTotal = zooAnimals.reduce((acc, animal) => { return acc += animal.population }, 0);
console.log(populationTotal);


// ==== Callbacks ====

/* Step 1: Create a higher-order function
* Create a higher-order function named consume with 3 parameters: a, b and cb
* The first two parameters can take any argument (we can pass any value as argument)
* The last parameter accepts a callback
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/
* Create a higher-order function named consume with 3 parameters: a, b and cb
* The first two parameters can take any argument (we can pass any value as argument)
* 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, cd) {
return cd(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!"
*/
* 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(first, last) {
return `Hello ${first} ${last}, 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 All @@ -78,5 +99,4 @@ console.log(populationTotal);

Stretch: If you haven't already, convert your array method callbacks into arrow functions.

*/

*/
24 changes: 22 additions & 2 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.

class CuboidMaker {
constructor(params) {
this.length = params.length;
this.width = params.width;
this.height = params.height;
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea() {
return 2 * ((this.length * this.width) + (this.length * this.height) + (this.width * this.height));
}
}

let 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.
21 changes: 16 additions & 5 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,40 @@
/* == 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.
*/

// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
let 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