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

completed sprint challenge #186

Open
wants to merge 1 commit 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,37 @@ Edit this document to include your answers after each question. Make sure to lea

1. Explain the differences between `.map`, `.reduce` and `.filter` and describe a use case for each.

The .map method calls a function on every element in an array and outputs a new one.
ex. capitalize all names in an array

The .reduce method reduces all elements in an array to a single value.
ex. return the sum of all numbers in an array

The .filter method creates a new array with all elements that pass a condition.
ex. return all numbers greater than 8

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

A callback function is a little baby function that gets passed into a higher order function. High order functions house babies.

3. Explain what a closure is.

Closure is when a function looks outside of its scope to read an undefined, local variable.

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

Implicit: 'this' preceeds a dot method and is bound to the object before the dot

Explicit: 'this' is bound with .call or .apply

Window/Global: 'this' is not yet defined and bound to the window

New: 'this' is bound to the instance an object is created within a construtor function

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

super() binds the prototypes of a parent class to its subclass

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

## Instructions
Expand Down
117 changes: 74 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ myFunction();

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


// closure



Expand All @@ -28,10 +28,12 @@ myFunction();

For example, `summation(4)` should return 10 because 1+2+3+4 is 10. Note, you may use a for loop for this function if you wish */

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

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


// 🦁🦁🦁 Topic 2: ADVANCED Array Methods 🦁🦁🦁
Expand All @@ -56,8 +58,12 @@ const zooAnimals = [
displayNames will 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(data){
const displayNames = []
data.forEach(
animal => displayNames.push(`name: ${animal.animal_name}, scientific: ${animal.scientific_name}`))
return displayNames
}


Expand All @@ -67,8 +73,10 @@ const zooAnimals = [
For example: ['jackal, asiatic', .....]
*/

function lowerCaseNames(/*Your Code Here*/){
/*Your Code Here*/
function lowerCaseNames(data){
const lowerCase = data.map(
(animal) => animal.animal_name.toLowerCase()
); return lowerCase
}


Expand All @@ -77,8 +85,10 @@ const zooAnimals = [
Using lowPopulationAnimals use .filter() to create a new array of objects which contains only the animals with a population of less than 5.
*/

function lowPopulationAnimals(/*Your Code Here*/){
/*Your Code Here*/
function lowPopulationAnimals(data){
const lowPop = data.filter(
(animal) => animal.population < 5
); return lowPop
}


Expand All @@ -88,8 +98,10 @@ const zooAnimals = [
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.
*/

function USApop(/*Your Code Here*/){
/*Your Code Here*/
function USApop(data){
const USApopReduced = data.reduce(
(total, animal) => total + animal.population
,0); return USApopReduced
}


Expand All @@ -101,90 +113,109 @@ const zooAnimals = [
* The consume function should return the invocation of cb, passing a and b into cb as arguments
*/

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(); 🦁🦁🦁 */
// 🦁🦁🦁 Use add to return the sum of two numbers 🦁🦁🦁

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


// 🦁🦁🦁 Use multiply to return the product of two numbers 🦁🦁🦁

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


// 🦁🦁🦁 Use greeting to accept a first and last name and return "Hello {first-name} {last-name}, nice to meet you!" 🦁🦁🦁

function greeting(/*Your Code Here */){
return /*Your Code Here */
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!
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 🐴🐴🐴 //
//🐴🐴🐴 Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are similar to cubes but do not have even sides. Follow the steps in order to accomplish this challenge. 🐴🐴🐴
/* 🐴🐴🐴 Step 1: Base Constructor 🐴🐴🐴
Use the constructor function named CuboidMaker to accept properties for length, width, and height which can be initialized as an object
*/
function CuboidMaker(/*Your Code Here */){
/*Your Code Here */
function CuboidMaker(cuboid){
this.length = cuboid.length;
this.width = cuboid.width;
this.height = cuboid.height
}


/* 🐴🐴🐴 Step 2: Volume Method 🐴🐴🐴
Create a method called volume 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 called surfaceArea 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 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
})

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


// 🦄🦄🦄 Topic 4: Classes 🦄🦄🦄 //
//🦄🦄🦄 1. Take your prototypes from above and refactor into class syntax. Please rename your class CuboidMakerTwo and your object cuboidTwo 🦄🦄🦄
class CuboidMakerTwo{

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
Loading