Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda