简体   繁体   中英

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

Right now you're setting the value 'isReserved' to false with this line:

if (campgrounds[i].isReserved = false){//If the campground is available 

= is a set, == is a test. Try this:

if (campgrounds[i].isReserved == false){//If the campground is available 

result = result + campgrounds[i].number should be inside the IF STATEMENT

Also, use == insead of =

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

You need to evaluate the conditional with the comparisson operator (==) instead the assignment operator (=), then push that value in the result array.

 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 } ] function availableCampsites(campgrounds){ let result = []; for (i = 0; i < campgrounds.length; i++){ if (campgrounds[i].isReserved == false){//If the campground is available result.push(campgrounds[i].number); } } return result; } console.log(availableCampsites(campgrounds))

result = result + campgrounds[i].number

This is not the correct way to add value to an array. Use this instead:

result.push(campgrounds[i].number)

Also you are using = operator, which is an assignment operator, not a comparison one.

So your final code becomes:

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

      } result.push(campgrounds[i].number);
  }
  return result;
}

You can do it more cleanly like this:

function availableCampsites(campgrounds){
  return campgrounds.filter(c => c.isReserved === false)
                    .map(c => c.number);
}

It first filters the array down to those that you want, and then it maps that filtered result to a new array of their number property.

Three problems I see here:

  1. you're not checking what you think you are in your condition - in fact, you're not checking anything: if (campgrounds[i].isReserved = false) This line is setting the value of camgrounds[i].isReserved to false. To check equality, you need to use == or === (you can look up the difference between the two yourself, here it shouldn't matter).

  2. Your line when you add to the result is outside the function braces. It needs to be within the {} that follow your condition (after you fix it as per 1 above)

  3. You shouldn't be adding to an array with + in Javascript. It strangely stringifies it and appends your + operand to the end. You want to use an array method, like push : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array result.push(campgrounds[i].number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM