Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.filter javascript and defining variable

ok so here is the question
i am trying to pull the name of an animal if the population is less than 5 using the .filter method. set lowPop as the new array but it always returning as undefined. im not sure why that is could anyone assist?

I have attached a picture to show what the question is asking along with my code. i am getting that my var is undefined and my code is returning a boolian true or false response. the expected response is in the picture. any assistance is greatly appreciated.

code and question with expected/error pic

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function lowPopulationAnimals(){
const lowPop = [];
zooAnimals.filter((element => {
lowPop.push(element.population <= 5 );
}));

console.log(lowPopulationAnimals);

>Solution :

Note that:

The filter() method creates a new array with all elements that pass
the test implemented by the provided function.

It is for create a new array, not for modifying the new array.

Also, you should not use push in array.filter, filter is running a test to the array and create the copy to all the element in the array that pass the test.

Also, you are directly assigning function_name to console which is incorrect.

Use:

function lowPopulationAnimals(){
let lowPop = [];
 lowPop = zooAnimals.filter(element =>element.population <= 5 )
console.log(lowPop);
}

lowPopulationAnimals();

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading