Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda