Skip to content

first push #1268

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

Open
wants to merge 7 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
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,49 @@ Edit this document to include your answers after each question. Make sure to lea

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

Both .forEach and .map are array methods that accept a callback to execute a set of instructions on each array element.

.map on an array returns a new array with the result of the callback function for each element of the original array.

.foreach also covers the callback function for each element of the array, but it does not return a new array like .map does.

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

A higher order functions take another function as argument. Callback is a function that is passed to another function for it to be invoked inside of it.

A higher order function receives a function as argument. Callback functions are passed as arguments to other functions.

3. What is closure?

Closure is the combination of a function bundled together(enclosed) with references to its surrounding state. Closures are created everytime a function is created, at function creation time.

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

1)Global Scope binding: when 'this' is used outside of any specific object.

2)New binding: when 'new' is used with constructors to build new objects.

3)Explicit binding: when using apply and call where we chose some object for some fucntion to copy parent's property.

4)Implicit binding: when 'this' is used with a specific declared object.

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

When creating a child class of a parent class. It is for the child class to access the methods and properties of the parent class for the child class to access the functions of the parent class.

### Task 1 - Project Set up

Follow these steps to set up and work on your project:
Make sure you clone the branch that the TK links to: the vnext branch, NOT master!

- [ ] Create a forked copy of this project.
- [ ] Add TL as collaborator on Github.
- [ ] Clone your OWN version of Repo (Not Lambda's by mistake!).
- [ ] Create a new Branch on the clone: git checkout -b `<firstName-lastName>`.
- [ ] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
- [ ] You are now ready to build this project with your preferred IDE
- [ ] Implement the project on your Branch, committing changes regularly.
- [ ] Push commits: git push origin `<firstName-lastName>`.
- [X] Create a forked copy of this project.
- [X] Add TL as collaborator on Github.
- [X] Clone your OWN version of Repo (Not Lambda's by mistake!).
- [X] Create a new Branch on the clone: git checkout -b `<firstName-lastName>`.
- [X] Create a pull request before you start working on the project requirements. You will continuously push your updates throughout the project.
- [X] You are now ready to build this project with your preferred IDE
- [X] Implement the project on your Branch, committing changes regularly.
- [X] Push commits: git push origin `<firstName-lastName>`.



Expand All @@ -59,22 +81,22 @@ Your finished project must include all of the following requirements:
#### Task A: Objects and Arrays

Test your knowledge of advanced array methods and callbacks.
* [ ] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully!
* [X] Use the [arrays-callbacks.js](challenges/arrays-callbacks.js) link to get started. Read the instructions carefully!

#### Task B: Closure
#### Task B: Closure DONE

This challenge takes a look at closures as well as scope.
* [ ] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully!
* [X] Use the [closure.js](challenges/closure.js) link to get started. Read the instructions carefully!

#### Task C: Prototypes

Create constructors, bind methods, and create cuboids in this prototypes challenge.
* [ ] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully!
* [X] Use the [prototypes.js](challenges/prototypes.js) link to get started. Read the instructions carefully!

#### Task D: Classes

Once you have completed the prototypes challenge, it's time to convert all your hard work into classes.
* [ ] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully!
* [X] Use the [classes.js](challenges/classes.js) link to get started. Read the instructions carefully!

In your solutions, it is essential that you follow best practices and produce clean and professional results. Schedule time to review, refine, and assess your work and perform basic professional polishing including spell-checking and grammar-checking on your work. It is better to submit a challenge that meets MVP than one that attempts too much and does not.

Expand All @@ -86,6 +108,6 @@ There are a few stretch problems found throughout the files, don't work on them

Follow these steps for completing your project:

- [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo).
- [ ] Add your team lead as a Reviewer on the Pull-request
- [X] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo).
- [X] Add your team lead as a Reviewer on the Pull-request
- [ ] TL then will count the HW as done by merging the branch back into master.
125 changes: 105 additions & 20 deletions challenges/arrays-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,117 @@
// 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",
},
];

console.log("-ADVANCED ARRAY METHODS-");
console.log("");

/* 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 = [];
console.log(displayNames);
let displayNames = [];

zooAnimals.forEach((item) => displayNames.push('Name: ${item.animal_name}, Scientific: ${item.scientific_name}')
);
console.log("The answer to Task #1 is: ", displayNames);
console.log("");

/* Request 2: .map()

The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut.

*/

const lowCaseAnimalNames = [];
console.log(lowCaseAnimalNames);
const lowCaseAnimalNames = zooAnimals.map((item) => item.animal_name.toLowerCase());
console.log("The answer to Task #2 is: ", lowCaseAnimalNames);
console.log("");

/* 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 = [];
console.log(lowPopulationAnimals);

const lowPopulationAnimals = zooAnimals.filter((item) => item.population < 5);
console.log("The answer to Task #3 is: ", lowPopulationAnimals);
console.log("");

/* 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.

*/

let populationTotal = 0;
console.log(populationTotal);
populationTotal = zooAnimals.reduce((accum, items) => {
return (accum = accum + items.population);
}, 0)
console.log("The answer to Task #4 is: ", populationTotal);
console.log("");
console.log("-CALL BACKS-");
console.log("");


// ==== Callbacks ====
Expand All @@ -58,18 +125,36 @@ console.log(populationTotal);
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/

function consume(a,b,callback) {
return callback(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(a, b) {
return a + b;
}

function multiply(a, b) {
return a * b;
}

function greeting(firstName, lastName) {
return 'Hello ${firstName} ${lastName}, nice to meet you.';
}
console.log("");

/* 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("The answer is: ", consume(2, 2, add)); // 4
console.log("");
console.log("The answer is: ", consume(10, 16, multiply)); // 160
console.log("");
console.log("The answer is: ", consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you!



Expand Down
49 changes: 46 additions & 3 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
console.log("-CLASSES-");
console.log("");

// 1. Copy and paste your prototype in here and refactor into class syntax.

class CuboidMakerX {
constructor(length, width, height) {
this.length = length;
this.width = width;
this.height = height;
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea() {
return(2 * (this.length * this.width * this.height));
}
}

var cuboid = new CuboidMakerX(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("Volume Task: ", cuboid.volume()); // 100
console.log("");
console.log("Surface Area Task: ", cuboid.surfaceArea()); // 130
console.log("");

// 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.

class cubeMaker extends CuboidMakerX {
constructor(dimension) {
super(dimension, dimension, dimension);
}
volume() {
return Math.pow(this.width, 3);
}
surfaceArea() {
return 6 * Math.pow(this.width, 2);
}
}

const myCube = new cubeMaker(4);
console.log("Stretch:")
console.log("");
console.log(myCube);
console.log("");
console.log(myCube.volume);
console.log("");
console.log(myCube.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.
19 changes: 18 additions & 1 deletion challenges/closure.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
console.log("");
console.log("-CLOSURE-");
console.log("");
// ==== Closures ====

/* Task 1: Study the code below and explain in your own words why nested function can access the variable internal. */
Expand All @@ -11,14 +14,28 @@ function myFunction() {

function nestedFunction() {
console.log(internal);
};
}
nestedFunction();
}
myFunction();

// Explanation:
/* nestedFunction is the Child of the myFunction Parent function. The Child function(nested function)can reach the Parent function to get the variable's value. Closure is made as the 'internal' variable is available in the Parent function's scope. And the Child function is able to reach out to the Parent function when the variable is not defined within it.
*/


/* 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. */

let counter = 0;
function summation(num) {
for (let i = 0; i <= num; i++) {
counter += i;
}
return counter;
}

console.log("A function using closure to get summation of the number 4:", summation(4));
console.log("");

Loading