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

.forEach() is good tool when you need to execute a function for each individual element in an array. The difference is that forEach iterates over an array and map iterates over an array and returns the result.

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

Functions and methods both are functions in JavaScript. A method is just a function which is a property of an object.

3. What is closure?

a closure gives you access to an outer function’s scope from an inner function. Closures are created every time a function is created, at function creation time.

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

The "Window" binding is when the "this" keyword is invoked in the global scope or not used in the object.
The "Implicit" binding is when the "this" keyword is called inside of the object this is the most common use of the "this" keyword.
The "New Binding" is when the "this" keyword is used in a constructor to create an object and its properties.
The "Explicit Binding" is when we can override what the constructor objects get set to by calling them using the .call and the .apply.
*

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

the super keyword is used as a function which calls the parent class.



## Project Set up

Follow these steps to set up and work on your project:
Expand Down
40 changes: 38 additions & 2 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.
// function CuboidMaker(attributes) {
// this.length = attributes.length;
// this.width = attributes.width;
// this.height = attributes.height;
// }

class CuboidMaker {
constructor(attributes) {
this.length = attributes.length;
this.width = attributes.width;
this.height = attributes.height;
}

volume() {
return (this.length) * (this.width) * (this.height);
}

surfaceArea() {
return 2 * ((this.length) * (this.width) + (this.length) * (this.height) + (this.width) * (this.height));
}
}

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

// CuboidMaker.prototype.surfaceArea = function () {
// return 2 * ((this.length) * (this.width) + (this.length) * (this.height) + (this.width) * (this.height));
// };



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

// 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.
45 changes: 41 additions & 4 deletions challenges/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,56 @@
* 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 consume(array, func) {
for (const i = 0; i < array.length; i++) {
return array[i];
}
}

function sumSquares(numbers) {
const total = 0;

return total;
}

sumSquares([1, 2, 3, 4]);

function add(x, y, cb) {

cb(x + y);
}

add(2, 2, (add) => {
console.log(add);
});

function multiply(x, y, cb) {

cb(x * y);
}

multiply(10, 16, (multiply) => {
console.log(multiply);
});

const firstName = 'Mary';
const lastName = 'Poppins';
function greeting() {
console.log(`Hello ${firstName} ${lastName}, nice to meet you!`);
}
greeting();

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


// ==== Closures ====

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

// Explanation:
// Explanation: The reason nestedfunction() can access the variable "internal" is because the variables and functions declared within the function can reach outward for context.


const external = "I'm outside the function";
Expand Down
90 changes: 81 additions & 9 deletions challenges/objects-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,51 @@

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



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

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

const velociraptor = {
"diet": "carnivorous",
"weight": "15kg",
"length": "1.8m",
"period": "Late Cretaceous"
};

// How much did tyrannosaurus weigh?
console.log();
console.log(Object.entries(tyrannosaurus)[1]);

// What was the diet of a velociraptor?
console.log();
console.log(Object.entries(velociraptor)[0]);

// How long was a stegosaurus?
console.log();
console.log(Object.entries(stegosaurus)[2]);

// What time period did tyrannosaurus live in?
console.log();
console.log(Object.entries(tyrannosaurus)[3]);


// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result.
console.log();
const letSpeak = function () {
return "RAWERSRARARWERSARARARRRR!";
}
tyrannosaurus.speak = letSpeak;
console.log(tyrannosaurus.speak());


// ==== Arrays ====
Expand All @@ -50,6 +80,13 @@ const graduates = [{"id":1,"first_name":"Cynde","university":"Missouri Southern

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

for (i = 0; i < graduates.length; i++) {
graduates[i].university;
universities.push(graduates[i].university);
universities.sort();
}

console.log(universities)

/* Request 2: Create a new array called contactInfo that contains both first name and email of each student.
Expand All @@ -59,12 +96,23 @@ Name [email protected]

Log the result of your new array. */
const contactInfo = [];

for (i = 0; i < graduates.length; i++) {
contactInfo.push(`${graduates[i].first_name} ${graduates[i].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);

for (let i = 0; i < graduates.length; i++) {
uni.push(graduates[i].university.match(/Uni/))
console.log(uni[i]);
}




// ==== ADVANCED Array Methods ====
Expand All @@ -89,6 +137,11 @@ The zoo wants to display both the scientific name and the animal name in front o

*/
const animalNames = [];

zooAnimals.forEach(element => {
animalNames.push(`names: ${element.animal_name}, scientific: ${element.scientific_name}`)
});

console.log(animalNames);

/* Request 2: .map()
Expand All @@ -98,22 +151,41 @@ The zoos need a list of all their animal's names (names only, not scientific) co
*/

const lowerCase = [];
console.log(lowerCase);

const name = []
let allCaps = [];
zooAnimals.forEach(function (item) {
lowerCase.push(item.animal_name);
});

console.log(lowerCase.map(item => item.toLowerCase()));


/* 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 lowerPopulation = [];
let lowerPopulation = [];

lowerPopulation = zooAnimals.filter(word => word.population < 5);
console.log(lowerPopulation);

/* 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 = 0;

const reducer = (accumulator, currentValue) => accumulator + currentValue;
let population = []
zooAnimals.forEach(function (item) {
population.push(item.population)
})


populationTotal = population.reduce(reducer);
console.log(populationTotal);


Expand Down
24 changes: 18 additions & 6 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,41 @@
/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/

function CuboidMaker(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
*/

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