I am trying to add an object key value to an empty array using a a for loop to filter, I keep getting an empty array

function findMyCampsites(campgrounds, view, partySize) {
  let matchingCampsites = []
  
  for (let i = 0; i < campgrounds.length; i++) {
    if (campgrounds[i].isReserved === false) {
      if (campgrounds[i].view === view) {
        if (campgrounds[i].partySize <= partySize) {
             matchingCampsites.push(campgrounds[i].number)
        } 
        return matchingCampsites
      } else {
        return "Sorry, no campsites with that view are available to host your party"
      }
    }
  }
     
}

const 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 }
];

I tried removing .number in the push statement, that didn't work, I get the right message when nothing matches and returns "Sorry no campsites......" but get back an empty array for everything else



from Recent Questions - Stack Overflow https://ift.tt/2MnOyAD
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations