Skip to content
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,30 @@ Edit this document to include your answers after each question. Make sure to lea

1. Describe the biggest difference between `.forEach` & `.map`.

1) .forEach executes a provided function once for each array element. 2) .map creates a new array with the results of calling a provided function on every element in the calling array.

2. What is the difference between a function and a method?

2) THey are both types of functions.

3. What is closure?

3) a variable within an upper level of a function

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

4) 1. If the new keyword is used when calling the function, this inside the function is a brand new object.

2. If apply, call, or bind are used to call a function, this inside the function is the object that is passed in as the argument.

3. If a function is called as a method — that is, if dot notation is used to invoke the function — this is the object that the function is a property of. In other words, when a dot is to the left of a function invocation, this is the object to the left of the dot. (ƒ symbolizes function in the code blocks)

4. If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it’s window.

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

5) to connect a child to a parent

## Project Set up

Follow these steps to set up and work on your project:
Expand Down
85 changes: 81 additions & 4 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.


// 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 ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/
class CuboidMaker2 {
constructor(attributes) {
this.length = attributes.length;
this.width = attributes.width;
this.height = attributes.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
*/

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




class Cubemaker extends CuboidMaker {
constructor(attributes) {
super(attributes);
}
volumeCube() {
return `the volume of the cube is ${this.length * this.length * this.length}`;
}
surfaceAreaCube() {
return `the area of the cube is ${6 * (this.length * this.length)}`;
}
}

const cuboid2 = new CuboidMaker2({
length: 4,
width: 5,
height: 5
});

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



// 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.
console.log(cuboid2.volume()); // 100
console.log(cuboid2.surfaceArea()); // 130

console.log(cube.volumeCube()); // 100
console.log(cube.surfaceAreaCube()); // 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.

37 changes: 29 additions & 8 deletions challenges/functions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


// ==== Callbacks ====

/* Step 1: Create a higher-order function that accepts a callback
Expand All @@ -7,24 +9,43 @@
* In the body of the function return the callback with the two parameters that you created
*/

function consume(num1, num2, callback) {
return callback(num1, num2);
}



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

const add = function (num1, num2) {
return num1 + num2;
};

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

const greeting = function (name1, name2) {
return `Hello ${name1} ${name2}, nice to meet you!`;
};


/* Step 3: Check your work by un-commenting the following calls to consume(): */
// consume(2,2,add); // 4
// consume(10,16,multiply); // 160
// 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!


// ==== Closures ====
// ==== Closures ====

// Explain in your own words why `nestedfunction()` can access the variable `internal`.

console.log(`the nestedfunction is nested past the point where the internal variable is... I other words the nestedfunction is in the closure.`)

// Explanation:


Expand Down
111 changes: 94 additions & 17 deletions challenges/objects-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,54 @@

// tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceious

let tyrannosaurus = {
name: "tyrannosaurus",
diet: "carnivorous",
weight: "7000kg",
length: "12m",
period: "Late Cretaceious",
roar: function() {
return (`RAWERSRARARWERSARARARRRR!`);
}
};

// stegosaurus, herbivorous, 2000kg, 9m, Late Jurassic

let stegosaurus = {
name: "stegosaurus",
diet: "herbivorous",
weight: "2000kg",
length: "9m",
period: "Late Jurassic"
};

// velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceious

let velociraptor = {
name: "velociraptor",
diet: "carnivorous",
weight: "15kg",
length: "1.8m",
period: "Late Cretaceious"
};

// Using your dinosaur objects, log answers to these questions:

// How much did tyrannosaurus weigh?
console.log();
console.log(tyrannosaurus.weight);

// What was the diet of a velociraptor?
console.log();
console.log(velociraptor.diet);

// How long was a stegosaurus?
console.log();
console.log(stegosaurus.length);

// What time period did tyrannosaurus live in?
console.log();
console.log(tyrannosaurus.period);


// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result.
console.log();
console.log(tyrannosaurus.roar());


// ==== Arrays ====
Expand All @@ -46,25 +73,62 @@ const graduates = [{"id":1,"first_name":"Cynde","university":"Missouri Southern
{"id":9,"first_name":"Michail","university":"Universidad Católica de Ávila","email":"mrome8@shinystat.com"},
{"id":10,"first_name":"Hube","university":"Universitat Rovira I Virgili Tarragona","email":"hlethbrig9@foxnews.com"}]

/* Request 1: Create a new array called universities that contains all the univeristies in the graduates array.
/* Request 1: Create a new array called universities that contains all the univeristies in the graduates array. */

let universities_list = [];
for (let k = 0 ; k < graduates.length; k++) {
universities_list.push(graduates[k].university);
}

console.log(universities_list);


// */ Once you have the new array created, sort the universities alphabetically and log the result. */
// const universities = [];
// console.log(universities)

const universities = universities_list.sort(function (a, b) {
if (a < b) return -1;
else if (a > b) return 1;
return 0;
});


Once you have the new array created, sort the universities alphabetically and log the result. */
const universities = [];
console.log(universities)



/* Request 2: Create a new array called contactInfo that contains both first name and email of each student.

The resulting contact information should have a space between the first name and the email information like this:
Name email@example.com

Log the result of your new array. */

const contactInfo = [];
for (let t = 0; t < graduates.length; t++) {
contactInfo.push(graduates[t].first_name);
contactInfo.push(graduates[t].email);
}
console.log(contactInfo);



/* Request 3: Find out how many universities have the string "Uni" included in their name. Create a new array called uni that contains them all. Log the result. */
const uni = [];
console.log(uni);

let uni = [];
for (let t = 0; t < graduates.length; t++) {
if (graduates[t].university.includes("Uni")){
uni.push(graduates[t]);
}
}
console.log(`\n There are`, uni.length, `Uni's in the list:`);
for (let ab = 0; ab < uni.length; ab++) {
console.log(uni[ab]);
}





// ==== ADVANCED Array Methods ====
Expand All @@ -88,32 +152,46 @@ zooAnimals = [{"animal_name":"Jackal, asiatic","population":5,"scientific_name":
The zoo wants to display both the scientific name and the animal name in front of the habitats. Return an array with only the animal and scientific names in it. The individual values in the array should look like this "Name: Jackal, asiatic, Scientific: Canis aureus."

*/
const animalNames = [];
console.log(animalNames);

let fullName = zooAnimals.forEach((name) => {
console.log(`name: ${name.animal_name}, Scientific: ${name.scientific_name}`);
// return `${runner.first_name} ${runner.last_name}`;

});


/* Request 2: .map()

The zoos need a list of all their animal's names (names only, not scientific) converted to lower case. Create a new array named lowerCase and map over each name to convert them all to lower case. Log the resut.

*/

const lowerCase = [];
const lowerCase = zooAnimals.map((currentValue) => {
return currentValue.animal_name.toLowerCase();
});
console.log(lowerCase);



/* Request 3: .filter()

The zoos are concenred about animals with a lower population count. Find out which animals have a population less than 5.

*/
const largerPopulation = [];
const largerPopulation = zooAnimals.filter((pop) =>{
return pop.population < 5;
});
console.log(largerPopulation);

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

*/
const populationTotal = 0;

let populationTotal = zooAnimals.reduce((accumulator, currentValue) => {
return accumulator + currentValue.population;
}, 0);

console.log(populationTotal);


Expand All @@ -122,4 +200,3 @@ console.log(populationTotal);
Stretch: If you haven't already, convert your array method callbacks into arrow functions.

*/

Loading