Sprint-Challenge--JavaScript Kseniya Platonava - #776
Conversation
Objects
Do forEach Do .map Do .filter Do .reduce
TheBrockstar
left a comment
There was a problem hiding this comment.
Objectives
MVP
Comprehension Questions
Answer the following questions to the best of your ability. You can exercise your Googling skills and use training kit. Open up the Answers.md file and record your responses there.
- 1. Describe the biggest difference between
.forEach&.map. - 2. What is the difference between a function and a method?
- 3. What is closure?
- 4. Describe the four rules of the 'this' keyword.
- 5. Why do we need super() in an extended class?
Objects and Arrays
- Objects
- Arrays
- Advanced Arrays
Functions
- Callbacks
- Closures
Prototypes
- Base Constructor
- Volume Method
- Surface Area Method
- New Object
Classes
- Base Constructor
- Volume Method
- Surface Area Method
- New Object
Stretch
- Objects and Arrays Stretch (Arrow Functions)
- Classes Stretch (CubeMaker)
Great
Kseniya, you have made such great progress. Well done on this project. You completed the MVP and Stretch.
Requests for Improvement
Please review the comments that I made and make sure that you understand them. If you do not, ask me so that I can clarify!
Commits
Your commits are beginning to look very professional. However, having only one word for each title is a bit difficult to read. In the future, use language like: "Finish Objects and Arrays" or something similar. Great job, regardless!
Rating: 3
| */ | ||
|
|
||
| const lowerCase = []; | ||
| lowerCase.push(zooAnimals.map(function(zooAnimal){return zooAnimal.animal_name.toLowerCase()})); |
There was a problem hiding this comment.
Here you are creating a new array (with the map array method) and then pushing that newly created array into the lowerCase array. So you're making an array of objects nested inside of another array, like this: [ [{'property': 'value'}] ] when it should just be an array of objects, like this: [{'property': 'value'}].
To resolve this, just assign the newly created array to the lowerCase variable, instead of pushing the newly created array into the array assigned to the lowerCase variable. Here is an example (I'm also using arrow notation), please let me know if you have any questions:
const lowerCase = zooAnimals.map(zooAnimal => zooAnimal.animal_name.toLowerCase());
| console.log(animalNames); | ||
|
|
||
| zooAnimals.forEach(function(zooAnimal){console.log(`Name: ${zooAnimal.animal_name}, Scientific: ${zooAnimal.scientific_name}.`)}) | ||
| console.log(animalNames); |
There was a problem hiding this comment.
While you are logging out the correct values here, the request is for you to create an array of the values that you are logging. Currently, you're just logging out a single string every time your forEach method loops. Then, when you log out the animalNames array with console.log(animalNames) you are logging out an empty array [].
The correct way to do this, is by using the push array method. Here is an example (using an arrow function), please let me know if you have any questions:
const animalNames = [];
zooAnimals.forEach(zooAnimal => {animalNames.push(`Name: ${zooAnimal.animal_name}, Scientific: ${zooAnimal.scientific_name}.`)})
console.log(animalNames);
| const largerPopulation = []; | ||
| const largerPopulation = []; //is is a trick to call the variable largePopulation and ask about small populations? | ||
|
|
||
| largerPopulation.push(zooAnimals.filter((zooAnimal)=>zooAnimal.population < 5)); |
There was a problem hiding this comment.
Similar to the above lowerCase issue, here you are creating a new array (with the map array method) and then pushing that newly created array into the largerPopulation array. So you're making an array of objects nested inside of another array, like this: [ [{'property': 'value'}] ] when it should just be an array of objects, like this: [{'property': 'value'}].
To resolve this, just assign the newly created array to the largerPopulation variable, instead of pushing the newly created array into the array assigned to the largerPopulation variable. Here is an example (I'm also using arrow notation), please let me know if you have any questions:
const largerPopulation = zooAnimals.filter(zooAnimal => (zooAnimal)=>zooAnimal.population < 5));
| */ | ||
| const populationTotal; | ||
| const populationTotal = []; | ||
| populationTotal.push(zooAnimals.reduce(function (sum, current) {return sum + current.population},0)); |
There was a problem hiding this comment.
Using push is fine here, but it is better to assign the value directly to populationTotal like:
const populationTotal = zooAnimals.reduce(function (sum, current) {return sum + current.population},0).
This is because an array is meant to hold a collection of values, whereas your reduce function is reducing down to and returning only a single value. Pushing that single value into an array is unnecessary.
| //sub class | ||
| class CubeMaker extends CuboidMaker{ | ||
| constructor(attributes){ | ||
| super(attributes); |
There was a problem hiding this comment.
While you've partially done this stretch goal correctly (great job!), this isn't really a CubeMaker. It's another CuboidMaker. The biggest difference between a Cube and a Cuboid is that all the sides of a Cube are the same length, but a Cuboid can have sides of different length. As such, a CubeMaker would only take one argument, as all of the sides would be equal to that one argument. Here is an example, please let me know if you have any questions:
class CubeMaker extends CuboidMaker {
constructor(length){
super(length);
this.length = length;
this.width = length;
this.height = length;
}
volume() {
return Math.pow(this.length, 3);
}
surfaceArea() {
return 6 * Math.pow(this.length, 2);
}
}
const cube = new CubeMaker(5);
Also take note of how the volume() and surfaceArea() methods are different.
@TheBrockstar