Confusion in forEach method in Js

hi guys https://codepen.io/anikettiwari/pen/poyJEZB , this is my solution to the challenge given in html.
In this i want to how can i find all those parks which have >= 1000 trees if their are multiple parks like this.

//3. Park with >= 1000 trees 

 //3.1. first get all the trees in an array
  const totalTrees = allParks.map(p => p.numTrees);

//3.2. then get all the indices of trees >=1000 trees in an array
  const moreIndex = totalTrees.map(r => r.findIndex(el => el >= 1000));

//3.3. then get all the parks with given indices 
  moreIndex.forEach(s => console.log(${arr[s].name} has more than 1000 trees.
    Precisely it has ${arr[s].numTrees}));  `

this is my solution, but it is showing some error and i’m not able to resolve it. what is my wrong with my approach and how can i solve it

Hey there!

Can you tell what’s the error and where are you submitting?

1 Like

I’m no expert but I’ve found a few dark sides I believe:

  1. Naming. I’ve changed constructors to singular. Example Parks --> Park. As when you call a function is for a singular element. Then I’ve moved allParks -->Parks.
  2. Return Value. I believe the density() method should return a value. So you can use the value later on. I’ve added that.
  3. findIndex according to a google search takes an array as input, but you were inputting a value. ( I could be wrong). findIndex was changed to a standard for loop. Surely you can improve that (maybe a filter, not sure).
  4. Arguments. Two functions are defined like this:
XYReport(arr){
Streets.forEach(...)
}

This should be

XYReport(arr){
arr.forEach(...)
}

And you call the function using XY(Streets)


Overall I got this.. I included comments for you.

I dont know where are you submitting so I cant check anything. The code looks very good anyways. Congrats.

1 Like

in the above code, my idea to get all parks with >= 1000 trees is -

  1. first map an array of all the trees totalTrees
  2. then among all these trees I want to filter out an array of indices of all the trees >= 1000 moreindex
  3. then I want to use these indices to print park name etc.
    following is my code to do the above thing , but I am not able to crack 2nd step -
const totalTrees = allParks.map(p => p.numTrees);

  // totalTrees.forEach(p=> console.log(p));

  const moreIndex = totalTrees.filter((value, index, array) =>{
//console.log(array.indexOf(value) >= 1000);
    return array.indexOf(value) >= 1000;
  });

     moreIndex.forEach(r => console.log(r));

console.log(array.indexOf(value) >= 1000); this thing is printing false false …
and I am not able to follow my plan

this is what i want exactly thank you for your solution but i am at learning stage so i need to correct my approach and so can you help me with my solution- where i went wrong , what could i have done

That seems like a lot of unnecessary work.

Why not just forEach the allParks array and check the numTrees property is 1000 or higher, then log the name and numTrees properties.

1 Like

did you mean something like this-

let result = allParks.filter( (value, index, array)=>{
return   array(value.numTrees) >= 1000;
}

result.forEach(p=> console.log(`${p.name} has more than 1000 trees. Precisely it has ${p.numTrees} trees`);

Why do you need filter? Why not just use an if statement inside the forEach loop.

i am thinking filter would return me array of all those parks which have more than 1000 trees wouldn’t that be easy to use filter to have that array

But why do you need the filtered array?

Unless you need the intermediate results of running different array methods or need reusable functions that will give you intermediate results to use as utility functions or for composition, there is no need to use the array methods just to use them.

I guess for practice it might make some sense to try and split the logic up into smaller pieces but for this particular case of finding the parks, I think just using a forEach with an if check should be enough.

happy anniversary :slight_smile:

1 Like

pardon which anniversary is it :thinking:

Mine, don’t you see the delicious cake next to my name?

Anyway, I guess I should have verbally responded to @anon10002461 so it was less confusing. Sorry.

hi i’ve come up with this code maybe it is same as @anon10002461 code , but can you analyse it or was there any easier way.

//3. Park with >= 1000 trees

 const moreThan1000 = [];

  for(var i=0 ; i<arr.length; i++){
    if(arr[i].numTrees >= 1000){
      //console.log(i);
      // console.log(arr[i]);
      moreThan1000.push(i);
    }
  }

    moreThan1000.forEach(p => console.log(`${arr[p].name} is a park with more than 1000 trees. Precisely it has ${arr[p].numTrees} trees`));

why you deleted this code ?
Btw Thank you for using that empty array, it didn’t come to my mind to create a new array .

uhmm probably, I can’t keep so many codepens. I can take a look in an hour if you need it. Did you pass the challenge or what’s the error ?

it was some sort of type error, i’d seriously be grateful if you’d look into my code. Maybe it is same as yours but still i just thought what else i can do to improve it. And also i 'm not able to understand how i can do it directly using allParks as @iasjorg suggested.

It’d make things way easier if you add the errors here, otherwise we’ll be reading 100 lines of code many times, which is not very fair :frowning:

A post was split to a new topic: Need help with adding local changes to GitHub