0

I'm working with an array, trying to create a function that determines which sites are available according to a given partySize and view, in other words which campsites are available that can fit the party size and have the given view. I am getting an output of 1 in my array, which is correct but it is not adding the number property for the second object in the campgrounds array.

The output in the array I am looking for with the current arguments is "[1,5]". I spent about 20 minutes messing with the code trying to find out what is going on. I tried changing some of my logic but it seems to be correct. Why exactly is the number for the second object not being pushed into the array? Thanks in advance!

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

function findMyCampsites(campgrounds, view, partySize){
  let sites = []
  for (let i = 0; i < campgrounds.length; i++){
    if (campgrounds[i].isReserved === false && campgrounds[i].view === view.toLowerCase()
      && campgrounds[i].partySize >= partySize) {
        sites.push(campgrounds[i].number)
        return sites
    } else {
      return 'Sorry, no campsites with that view are available to host your party'
    }
  }
}
console.log(findMyCampsites(campgrounds, 'ocean', 4))
  • `else { return` will mean your function always returns after a single iteration of the loop. Let the loop finish first - or, even better, use `.filter` instead – CertainPerformance Jun 06 '22 at 02:19

1 Answers1

0

Notice that you are calling return within your for statement

Instead you should move it out:

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

function findMyCampsites(campgrounds, view, partySize) {
  const sites = [];
  for (let i = 0; i < campgrounds.length; i++) {
    if (
      campgrounds[i].isReserved === false &&
      campgrounds[i].view === view.toLowerCase() &&
      campgrounds[i].partySize >= partySize
    ) {
      sites.push(campgrounds[i].number);
    }
  }

  if (!sites.length) {
    return 'Sorry, no campsites with that view are available to host your party';
  }

  return sites;
}

const result = findMyCampsites(campgrounds, 'ocean', 4);

console.log(result);

Also, i would recommend a more functional solution using Array.prototype.find() and Array.prototype.map()

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 },
]

const findMyCampsites = (campgrounds, view, partySize) => {
  const sites = campgrounds
    .filter(
      (s) =>
        s.isReserved === false &&
        s.view === view.toLowerCase() &&
        s.partySize >= partySize
    )
    .map(({ number: n }) => n)

  if (!sites.length) {
    return 'Sorry, no campsites with that view are available to host your party'
  }

  return sites
}

const result = findMyCampsites(campgrounds, 'ocean', 4)

console.log(result);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46