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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ 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 by your project manager

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent answers!

.forEach is an array method that carries out a function on each element, on the current array its applied on. .map carries out a function on every element in the specified array, but it returns a new array and doesn't modify the existing array.

2. What is the difference between a function and a method?
methods are simply functions that are declared inside of objects.

3. What is closure?
closure is a concept in javascript that allows nested functions to access variables outside of it's function scope inside the parent function or even from the global context. It is allowed to provide functions the ability to behave the way they were intended.

4. Describe the four rules of the 'this' keyword.
This has different meanings in different contexts. the meanings pertain to what is known as the 'binding' of the THIS keyword. There are 4 major bindings possible for THIS:

1. Default or Window Binding: when you do a console.log(this), the default behavior is that this is pointing to the global object, which is the global context.
2. Implicit or Automatic Binding: when you declare THIS in an object, this refers to the object it's contained in automatically.
3. Explicit Binding: when you use a function method to explicitly bind the THIS keyword to a specific object. for example: myFunction.bind(object_name); this would bind the this keyword to that specific object inside of the function. If you declared the this keyword inside a function, by default, it will point to the global or window context.
4. New Binding, using the new keyword: when using a constructor function, the new keyword that was used to create an object becomes what THIS refers to.


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

with the super() we put it into a constructor function, so we are able to access the parent class's properties and methods. we pass into super as an argument all of the properties of the child so the parent understands that the child has those specific properties.

## Project Set up

Follow these steps to set up and work on your project:
Expand Down
48 changes: 47 additions & 1 deletion challenges/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,50 @@
// 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.
// 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.


// 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 CuboidMaker {
constructor(keys) {
this.name = keys.name;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome that you added an additional property here

this.length = keys.length;
this.width = keys.width;
this.height = keys.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 `The volume of the ${this.name} is ${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 `The Surface Area of the ${this.name} is ${2 * (this.length * this.width + this.length * this.height + this.width * this.height)}`;
}
}

const cube = new CuboidMaker({name: 'cube', length: 5, width: 3, height: 10});
console.log(cube.surfaceArea());

/* == 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({name: 'cuboid', 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

27 changes: 23 additions & 4 deletions challenges/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* In the body of the function return the callback with the two parameters that you created
*/

function consume(arg1,arg2,cb) {
return cb(arg1,arg2);
}


/* Step 2: Create several functions to callback with consume();
* Create a function named add that returns the sum of two numbers
Expand All @@ -15,17 +19,32 @@
*/


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

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

function greeting(first,last) {
return `Hello ${first} ${last}, 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(`\nThe result of calling the consume function with the add callback function: ${consume(2,2,add)}\n`); // 4
console.log(`The result of calling the consume function with the multiply callback function: ${consume(10,16,multiply)}\n`);
console.log(`The result of calling the consume function with the greeting callback function: ${consume("Mary","Poppins", greeting)}\n`);



// ==== Closures ====

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

// Explanation:
console.log(`Explanation: nestedFunction() is able to access the variable internal, even though it is outside of the function scope. This is because of the concept in javascript known as a closure. Javascript allows for this behavior in order to allow functions to behave correctly. Technically, a function cannot access variables outside of it's scope. But because of the closure, a nested function can access variables in functions outside of it, and even in the global context.\n`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent answer!



const external = "I'm outside the function";
Expand Down
91 changes: 77 additions & 14 deletions challenges/objects-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,56 @@
object name, diet, weight, length, period
*/


// tyrannosaurus, carnivorous, 7000kg, 12m, Late Cretaceious
const tyrannosaurus = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work creating objects!

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work adding in the roar method!

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



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

// velociraptor, carnivorous, 15kg, 1.8m, Late Cretaceious
const 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to add the roar method!

console.log();

console.log(tyrannosaurus.roar());

// ==== Arrays ====

Expand All @@ -50,7 +76,12 @@ 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 = [];
console.log(universities)

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

console.log(universities.sort());

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

Expand All @@ -59,13 +90,27 @@ Name email@example.com

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

console.log('\n');
for (let 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);

console.log('\n');
for (let i = 0; i < graduates.length; i++){
if(graduates[i].university.includes('Uni')) {
uni.push(graduates[i].university);
}
}

console.log(uni);
console.log('\n');

// ==== ADVANCED Array Methods ====

Expand All @@ -83,12 +128,16 @@ zooAnimals = [{"animal_name":"Jackal, asiatic","population":5,"scientific_name":
{"animal_name":"Hawk-eagle, crowned","population":10,"scientific_name":"Spizaetus coronatus","state":"Florida"},
{"animal_name":"Australian pelican","population":5,"scientific_name":"Pelecanus conspicillatus","state":"West Virginia"}];


/* Request 1: .forEach()

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 = [];

zooAnimals.forEach( (animal,i) => animalNames.push('Name: ' + zooAnimals[i].animal_name,'Scientific: ' + zooAnimals[i].scientific_name));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome that you are using arrow syntax!


console.log(animalNames);

/* Request 2: .map()
Expand All @@ -97,29 +146,43 @@ The zoos need a list of all their animal's names (names only, not scientific) co

*/

const lowerCase = [];
//zooAnimals.forEach( (animal,i) => lowerCase.push(zooAnimals[i].animal_name.toLowerCase()));

const lowerCase = zooAnimals.map( (animal,i) => animal.animal_name.toLowerCase() );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! It doesn't look like you're using the second parameter in your callback function, so you could remove it


console.log('\n The zoo animals\' names all lowercased: ');
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 = [];
console.log(largerPopulation);

const smallerPopulation = zooAnimals.filter((animal,i) => zooAnimals[i].population < 5).map( (animal,i) => (`${animal.animal_name}, Population: ${animal.population}`));

//const smallerNames = smallerPopulation.map( (animal,i) => (`${smallerPopulation[i].animal_name}, Population: ${smallerPopulation[i].population}`));

console.log('\n An array of the zoo animals that have a population less than 5: \n');
console.log(smallerPopulation);



/* 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;
console.log(populationTotal);
let populationTotal = 0;

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

console.log('\n The total population for all animals in the zoo is: ' + populationTotal + '\n');


/*

Stretch: If you haven't already, convert your array method callbacks into arrow functions.

//all callback functions within the array methods already use arrow functions.
*/

2 changes: 2 additions & 0 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* ===== Prototype Practice ===== */

console.log('Currently, I am not able to write out the syntax using the classical prototypes and constructor functions because I was not able to grasp the concept or the syntax. I understood the concept and syntax for classes so I completed this task using classes in the classes.js file. I plan on using outside resources to learn more about constructor functions and prototypes and the this keyword. I hope to do the project for Javscript III and submit it hopefully.');

// 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 ==
Expand Down