mardi 11 août 2020

Can't figure out how to return the correct array

I have a problem and I'm having trouble finding the optimal approach to it. Here i want to be able to quickly find the campsites that's available. I am trying to return an array with the campsites numbers of all currently unreserved. I thought I had the code all figured out until i see that my code seems to be returning strings rather than the value.

function availableCampsites(campgrounds){
  let result = [];
  for (i = 0; i < campgrounds.length; i++){
    if (campgrounds[i].isReserved = false){//If the campground is available 

    } result = result + campgrounds[i].number
  }
  return result;
  }

and Here's the object from campground. I am trying to return the number variable from all of the unreserved campgrounds. This is an example of what I am trying to return. [1,5,12,18,23]

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

Aucun commentaire:

Enregistrer un commentaire