Skip to content

finished with the README questions #1250

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 10 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,40 @@ 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)

.forEach is a method that executes a function once for each array element, and .map is another method that creates a new array with the result of calling a provided funciton on every element in the calling array. One of the different between them is that .forEach doesnt return anything, but .map always return a new array of the same size. .forEach will change our original array, and .map will keep the same array, returning a new array. One of the similarity of both methods is that they will call a provided function on every element of the array, and also because they are both array iterations.

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

A higher order function is the main function that can have a function inside it, and use it as an argument. and a callback function is the child function of the main function(this case the higher order function), expecting it to call it.

3. What is closure?

closure is the combination of a function, working together with its lexical environment(its surrounding). A closure can give us access to an outsider function scope from a inner function. also, if there is not a variable defined inside the function scope, it will look outside the scope, searching for a variable being referenced outside the function scope.

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

1. Global binding: its used when a function is contained in the globa scope, and this value inside the function will be the window object.
2. Implicit binding: its used when a function is called by a precedent dot, and the object before that dot is this.
3. New binding: its used when it creates a new object based on a constructor function, and later, it passing in new arguments.
4. Explicity Binding: its used when we use .call, and .apply to create new objects from previous objects.

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

we use super() in an extended class because by using this method, we will call the parent's constructor method, and will get access to the parent's propierties and methods.

### 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 +72,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

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 +99,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
- [ ] TL then will count the HW as done by merging the branch back into master.
- [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
- [ x] TL then will count the HW as done by merging the branch back into master.
52 changes: 44 additions & 8 deletions challenges/arrays-callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ The zoos want to display both the scientific name and the animal name in front o

*/
const displayNames = [];
// zooAnimals.forEach( function(array) {
// return displayNames.push(`Name: ${array.animal_name}, Scientific: ${array.scientific_name}`);
// });
//stretch with arrow functions

zooAnimals.forEach((array) => displayNames.push(`Name: ${array.animal_name}, Scientific: ${array.scientific_name}`));

console.log(displayNames);

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

const lowCaseAnimalNames = [];
// zooAnimals.map( function(array) {
// return lowCaseAnimalNames.push(array.animal_name.toLowerCase());
// });
//stretch with arrow functions

zooAnimals.map((array) => lowCaseAnimalNames.push(array.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( function(array) {
// return array.population < 5;
// });
//stretch with arrow functions

const lowPopulationAnimals = zooAnimals.filter((array) => array.population < 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;
// const populationTotal = zooAnimals.reduce( function(total, array) {
// return total + array.population
// },0);
//stretch with arrow functions

const populationTotal = zooAnimals.reduce((total, array) => total + array.population,0);

console.log(populationTotal);


Expand All @@ -57,21 +83,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(sum1, sum2) {
return sum1 + sum2;
};

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

function multiply(num1, num2) {
return num1 * num2;
};

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!


/*
Expand Down
76 changes: 72 additions & 4 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.

// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/
class CuboidMaker {
constructor (attrs) {
this.length = attrs.length;
this.width = attrs.width;
this.height = attrs.height;
};

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

/* == 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 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 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({
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

// 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 CuboidMaker {
constructor(cubeAttrs) {
super(cubeAttrs);
};
//formula to find the volume of a cube is v = length * width * height
volume() {
return this.length * this.width * this.height;
};

// formula to fnd surface area of a cube is sa: 2 * (length * width + length * height + width * height)
surfaceArea() {
return 2 * (this.length * this.width + this.length * this.height + this.width * this.height)
};
};

const cube = new CubeMaker ({
length: 4,
width: 4,
height: 4
});

console.log(cube.volume()); //64
console.log(cube.surfaceArea()); //96
15 changes: 14 additions & 1 deletion challenges/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ function myFunction() {
}
myFunction();

// Explanation:
// Explanation: nested function can access the variable internal because they both are inside the same function myFunction scope. Also, it is a closure because nestedFunction is using a variable that is outside its function byt aroung its lexical environment.


/* 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 summation(num) {
let sum = 0;
return function() {
for(let i=1; i <= num; i++) {
sum += i;
}
return sum;
}
};

const sumTotal = summation(4);
console.log(sumTotal());
4 changes: 2 additions & 2 deletions challenges/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<title>Sprint Challenge</title>

<script src="objects-arrays.js"></script>
<script src="functions.js"></script>
<script src="arrays-callbacks.js"></script>
<script src="closure.js"></script>
<script src="prototypes.js"></script>
<script src="classes.js"></script>
</head>
Expand Down
24 changes: 21 additions & 3 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,47 @@
/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/

function CuboidMaker(attrs) {
this.length = attrs.length;
this.width = attrs.width;
this.height = attrs.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({
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