Skip to content
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,29 @@ 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.

1. Explain the differences between `.map`, `.reduce` and `.filter` and describe a use case for each.
<!-- '.map' creates a new array by transforming every element in an array individually. You would use '.map' if you have an array of objects and you want to create a new array with specific keys from those objects

'.filter' creates a new array by removing elements that don't belong. '.filter' is used when you want to create a new array with only some of the objects in the original array.

'.reduce' takes all of the elements in an array and reduces them into a single value. '.reduce' is used to generate a single vaule or object from an array. -->

2. Explain the difference between a callback and a higher order function.
<!-- A callback is what is passed into a function. A higher order function recieves a function as a parameter or argument. -->

3. Explain what a closure is.
<!-- a closure gives you access to an outer function's scope from an inner function -->

4. Describe the four principles of the 'this' keyword.
<!-- Window binding: if we haven't given 'this' any context it will return the window, the global object in node or undefined in strict mode.

Implicit binding: Applies to objects with methods. When the function is invoked, whatever is left of the dot, that's what 'this' refers to.

Explicit binding: we tell a function what the 'this' keyword should be using .call, .apply or .bind.

New binding: When a function is invoked with a new keyword, 'this' is bound to the new object being constructed. When a function is invoked as a constructor function using the new keyword, this points to the new object that’s created -->

5. Why do we need super() in an extended class?
<!-- The extends will inherit parent properties and will abstract away call() function. The super()function will inherit parent method to child and will not ever need it to bind. Also abstract away any of syntax that we have using to being our objectS prototype. -->

You are expected to be able to answer questions in these areas. Your responses contribute to your Sprint Challenge grade.

Expand Down
111 changes: 80 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function myFunction() {

//🚀🚀🚀 ⬇️ 📝 Explanation ⬇️ 📝 🚀🚀🚀:


// nestedFunction can access internal because it is within the scope of myFunction.



Expand All @@ -30,10 +30,13 @@ function myFunction() {
💡 NOTE: you may use a for loop for this function if you wish
*/

function summation(/*Your Code Here*/) {
/*Your Code Here*/

function summation(number) {
let sum = 0;
for (let i = 1; i <= number; i++) {
sum += i;
}
return sum;
}


// 🦁🦁🦁 Topic 2: ADVANCED Array Methods 🦁🦁🦁
Expand All @@ -60,8 +63,12 @@ const zooAnimals = [
💡 NOTE: the array returned should be an array of strings, and each string should follow this pattern: "name: {name}, scientific: {scientific name}"
*/

function animalNames(/*Your Code Here*/){
/*Your Code Here*/
function animalNames(array){
const displayNames = [];
array.forEach(function(item, index){
displayNames.push(`name: ${item.animal_name}, scientific: ${item.scientific_name}`);
});
return displayNames;
}


Expand All @@ -75,8 +82,12 @@ const zooAnimals = [
💡 NOTE: Do some research for other methods that can help help you
*/

function lowerCaseNames(/*Your Code Here*/){
/*Your Code Here*/
function lowerCaseNames(array){
const lowerCase = [];
array.map(function(item){
lowerCase.push(item.animal_name.toLowerCase());
});
return lowerCase;
}


Expand All @@ -88,8 +99,11 @@ const zooAnimals = [
3. Return this new array
*/

function lowPopulationAnimals(/*Your Code Here*/){
/*Your Code Here*/
function lowPopulationAnimals(array){
array.filter(function(item){
return item.population < 5;
});
return array;
}


Expand All @@ -102,8 +116,11 @@ const zooAnimals = [
💡 NOTE: Remember the reduce method takes two arguments: a callback (which itself takes two args - the accumulator and the item), and an initial value for the count. Check MDN/W3Schools for syntax!
*/

function USApop(/*Your Code Here*/){
/*Your Code Here*/
function USApop(array){
const sum = array.reduce(function(acc, item){
return acc + item.population;
}, 0);
return sum;
}


Expand All @@ -116,9 +133,9 @@ const zooAnimals = [
💡 NOTE: The tests for 'consume' will pass if it is created correctly and also after you correctly complete the functions 'add' and 'greeting' below in Step 2.
*/

function consume(/*Your Code Here */){
/*Your Code Here */
}
function consume(a, b, cb){
return cb(a, b);
}


// 🦁🦁🦁 Step 2: Create several functions to callback with consume(); 🦁🦁🦁
Expand All @@ -128,19 +145,19 @@ const zooAnimals = [
2. Return the sum of those numbers
*/

function add(/*Your Code Here */){
/*Your Code Here*/
}
function add(a, b) {
return a + b;
}


/* Use multiply to do the following:
1. Receive two numbers as an argument that are passed in from its first and second parameters
2. Return the product of those numbers
*/

function multiply(/*Your Code Here */){
/*Your Code Here */
}
function multiply(a, b) {
return a * b;
}


/* Use greeting to do the following:
Expand All @@ -149,9 +166,9 @@ function multiply(/*Your Code Here */){
💡 NOTE: The string returned must match the format above or the test will not pass!
*/

function greeting(/*Your Code Here */){
return /*Your Code Here */
}
function greeting(first_name, last_name) {
return `Hello ${first_name} ${last_name}, nice to meet you!`;
}


// 🦁🦁🦁 Step 3: Check your work by un-commenting the following calls to consume(): 🦁🦁🦁
Expand All @@ -160,7 +177,9 @@ function greeting(/*Your Code Here */){
// 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!

// 🐴🐴🐴 Topic 3: Prototypes 🐴🐴🐴 //

Expand All @@ -175,8 +194,10 @@ function greeting(/*Your Code Here */){
- Instances of CuboidMaker should initialize `length`, `width` and `height` properties
*/

function CuboidMaker(/*Your Code Here */){
/*Your Code Here */
function CuboidMaker(cuboid){
this.length = cuboid.length;
this.width = cuboid.width;
this.height = cuboid.height
}


Expand All @@ -185,6 +206,9 @@ function CuboidMaker(/*Your Code Here */){
💡 NOTE: Formula for cuboid volume: length * width * height
*/

CuboidMaker.prototype.volume = function () {
return this.length * this.width * this.height
}



Expand All @@ -193,14 +217,20 @@ function CuboidMaker(/*Your Code Here */){
💡 NOTE: Formula for cuboid surface area: 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 (not auto graded)🐴🐴🐴
Create an object called cuboid 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
})



Expand All @@ -209,22 +239,41 @@ function CuboidMaker(/*Your Code Here */){
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130

console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130

// 🦄🦄🦄 Topic 4: Classes 🦄🦄🦄 //
//Using CuboidMakerTwo, take your prototypes from above and refactor into class syntax. Then, create an object called cuboidTwo that uses the new keyword to use our CuboidMakerTwo class.

class CuboidMakerTwo{

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

const cuboidTwo = new CuboidMakerTwo({
length: 4,
width: 5,
height: 5
})




//🦄🦄🦄 Test your volume and surfaceArea methods by uncommenting the logs below: 🦄🦄🦄
// console.log(cuboidTwo.volume()); // 100
// console.log(cuboidTwo.surfaceArea()); // 130


console.log(cuboidTwo.volume()); // 100
console.log(cuboidTwo.surfaceArea()); // 130



Expand Down