mercredi 12 août 2020

how to get the total array of object

So what I am trying to do for this project is create a function which is "findMyCampsites" That takes in three arguments which are campgrounds, views, and partySize. I am trying to get my code to take in an persons view preference "forest" or "ocean" and well as the size of there expected party and return the number of all available campgrounds matching the user input. But if there are no matches the function should return an empty array. Here is what i was able to come up with so far.. I can't figure out why i am returning [] no matter what. What am i doing wrong?

> function findMyCampsites(campgrounds, view, partySize){
  let result = []
 for (i = 0; i < campgrounds.length; i++){
   if (campgrounds[i].isReserved === false){
      if (campgrounds[i].view === view){
         result.push(campgrounds[i].view)
       if (campgrounds[i].partySize === partySize){
       result.push(campgrounds[i].partySize)
     result = result + 1;
   

     }  
   } 
 }return result;

 }

}

also here is my campground object

> let campgrounds = [
  { number: 1, view: 'ocean', partySize: 8, isReserved: false },
  { number: 5, view: 'ocean', partySize: 4, isReserved: false },
  { number: 12, view: 'ocean', partySize: 4, isReserved: true },
  { number: 18, view: 'forest', partySize: 4, isReserved: false },
  { number: 23, view: 'forest', partySize: 4, isReserved: true }
]

Aucun commentaire:

Enregistrer un commentaire