// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems.

const zooAnimals = [

{ animal_name: "Jackal, asiatic", population: 5, scientific_name: "Canis aureus", state: "Kentucky" },

{ animal_name: "Screamer, southern", population: 1, scientific_name: "Chauna torquata", state: "Alabama" },

{ animal_name: "White spoonbill", population: 8, scientific_name: "Platalea leucordia", state: "Georgia" },

{ animal_name: "White-cheeked pintail", population: 1, scientific_name: "Anas bahamensis", state: "Oregon" },

{ animal_name: "Black-backed jackal", population: 2, scientific_name: "Canis mesomelas", state: "Washington" },

{ animal_name: "Brolga crane", population: 9, scientific_name: "Grus rubicundus", state: "New Mexico" },

{ animal_name: "Common melba finch", population: 5, scientific_name: "Pytilia melba", state: "Pennsylvania" },

{ animal_name: "Pampa gray fox", population: 10, scientific_name: "Pseudalopex gymnocercus", state: "Connecticut" },

{ 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 zoos want to display both the scientific name and the animal name in front of the habitats. Populate the displayNames array with only the animal_name and scientific_name of each animal. displayNames will be an array of strings, and each string should follow this pattern: "Name: Jackal, asiatic, Scientific: Canis aureus."

*/

const displayNames = [];

console.log(displayNames);

/* Request 2: .map()

The zoos need a list of all their animal's names (animal_name only) converted to lower case. Using map, create a new array of strings named lowCaseAnimalNames, each string following this pattern: "jackal, asiatic". Log the resut.

*/

const lowCaseAnimalNames = [];

console.log(lowCaseAnimalNames);

/* Request 3: .filter()

The zoos are concerned about animals with a lower population count. Using filter, create a new array of objects called lowPopulationAnimals which contains only the animals with a population less than 5.

*/

const lowPopulationAnimals = [];

console.log(lowPopulationAnimals);

/* 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. Remember the reduce method takes two arguments: a callback (which itself takes two args), and an initial value for the count.

*/

const populationTotal = 0;

console.log(populationTotal);

/*

Respuesta :

Answer:

1. .forEach( )

// displayNames is the array to hold the name of the animal

const displayNames = [];

// forEach method is called on zooAnimals array. Inside the forEach method

// a String variable temp is declared to hold the name and scientific name of

// the animal in a specified format. Then the string is push into the array.

zooAnimals.forEach(function(element){

   var temp = "Name: " + element.animal_name + ", " + "Scientific: " + element.scientific_name

   displayNames.push(temp)

})

// the array displayNames is log to the console

console.log(displayNames);

2. .map( )

// The map method return an array and is assigned to lowCaseAnimalNames

// Arrow function is use to perform the mapping. It map each element animal

// name to lower case during each iteration in the array

const lowCaseAnimalNames = zooAnimals.map(element => element.animal_name.toLowerCase());

// the array lowCaseAnimalNames is log to the console

console.log(lowCaseAnimalNames);

3. .filter( )

// The filter method of an array return an array. So, it is assign to

// lowPopulationAnimals. The Arrow function (=>) is used to return only object

// of animals with population less than 5

const lowPopulationAnimals = zooAnimals.filter(element => element.population < 5);

// log the population of animals less than 5 to the console

console.log(lowPopulationAnimals);

4. .reduce( )

// The reduce method of an array also return a single number after operation

// The reduce method has two arguments (accumulator and currentValue)

// The accumulator hold the value of our initial sum which is 0. The

// currentValue represent the current element been handled in the loop

const populationTotal = zooAnimals.reduce((accumulator, currentValue) => accumulator + currentValue.population, 0);

// log the total population of animal in the zoo

console.log(populationTotal);

Explanation:

ACCESS MORE
EDU ACCESS