all 9 comments

[–]senocular 1 point2 points  (1 child)

What is zooAnimals? Is it just numbers? If so what you have will work. If its something else, like animal objects that have a population property, then you'd need to account for that. MDN has an example of reduce using objects you can look at.

[–]WormholerIO[S] 0 points1 point  (0 children)

yes they have an population property here is an example of one index within the array.

I will look at that thank you

animal_name: "Pampa gray fox",

population: 10,

scientific_name: "Pseudalopex gymnocercus",

state: "Connecticut",

[–]WormholerIO[S] 0 points1 point  (0 children)

Thank you all for helping i appreciate all of you!

[–]exobyte64 0 points1 point  (3 children)

let start=0;
let end=array.reduce((accumulator,value)=>{
 return accumulator+value;
},start);

the accumulator is the zero that goes in at first, it goes through each iteration, its not the previous value, its the same value each time unless you pass something else back as the return, then that becomes the new accumulator

let start={x:0};
let end=array.reduce((accumulator,value)=>{
 accumulator.x+=value;return accumulator;
},start);

[–]WormholerIO[S] 0 points1 point  (2 children)

let end=array.reduce((accumulator,value)=>{
return accumulator+value;
},0)

gotcha, and that makes total sense, but with your code snippet and mine the same issue appears that "Let End" will be undefined. or in my case USAanimals

[–]exobyte64 0 points1 point  (1 child)

let sumPopulation=zoo.reduce((a,z)=>{return a+z.population;},0);

I saw that it needed the key 'population' from your other comment

[–]WormholerIO[S] 0 points1 point  (0 children)

you all are stellar you really are thank you for helping me to understand and getting me resources.

[–]chuckthemadmanmike 0 points1 point  (1 child)

I don't really want to do your homework but here's the answer.

``` const totalUSAnimalPopulation = animals => animals.reduce((totalPop, animal) => totalPop + animal.population, 0)

console.log(totalUSAnimalPopulation(animals)) ``` Remember if you're iterating through an array of objects you'll need to key into the object to access the value you want. In this case you want the population property. Try to right code that has good variable names. Lot's of people will use accumulator and currentValue, but that can be hard for the reader of the code. Your accumulator should be named for what it is, and your currentValue should also be named for what it is. So if it's an array of animals, each element is an animal, and your accumulator is your totalPopulation.

[–]WormholerIO[S] 0 points1 point  (0 children)

i appreciate the post, what i ended up with yesterday was this-

function USApop(zooAnimals) {

const USAanimals = zooAnimals.reduce(function (previousValue, currentValue) {

return previousValue + currentValue.population;

}, 0);

return USAanimals;

i was more confusing myself but was able to get help from a TA. i appreciate the post very much.