Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Property of first object in array being pushed but not second object

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!