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))