skip to Main Content

This is the output error I am receiving

structure for campground variable is below–>

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 findMyCampsites(campgrounds,view,partySize){
  let siteNumbers=[];
    for (let i=0;i<campgrounds.length;i++){
      if (campgrounds[i].isReserved === false && campgrounds[i].view===view && campgrounds[i].partySize>=partySize)
        siteNumbers.push(campgrounds[i].number);       
 
else if (campgrounds[i].isReserved!==false && campgrounds[i].view!== view && campgrounds[i].partySize !== partySize)
 "Sorry, no campsites with that view are available to host your party";  }           
    return siteNumbers;  
      findMyCampsites(campgrounds,"ocean",4,8);
       findMyCampsites(campgrounds,"forest",4);  }

Every line of code throughout this sequence is executing properly EXCEPT the lines containing
The issue here is that my "else if" statement is not returning the string after it completes the "for loop", I am getting a return appearing as "[]" rather than my string.

else if (campgrounds[i].isReserved!==false && campgrounds[i].view!== view && campgrounds[i].partySize !== partySize)
 "Sorry, no campsites with that view are available to host your party";  }

If somebody has an idea what I am missing here please feel free to comment thoughts and ideas. Thank you.

2

Answers


  1. Your logic seems to be wrong before the readability issue.

         if (campgrounds[i].isReserved === false && campgrounds[i].view===view && campgrounds[i].partySize>=partySize)
            siteNumbers.push(campgrounds[i].number);       
    else if (campgrounds[i].isReserved!==false && campgrounds[i].view!== view && campgrounds[i].partySize !== partySize)
    

    will be..

    
    function findMyCampsites(campgrounds, view, partySize) {
      let siteNumbers = []
      for (let i = 0; i < campgrounds.length; i++) {
        if (campgrounds[i].isReserved === false && campgrounds[i].view === view && campgrounds[i].partySize >= partySize)
          siteNumbers.push(campgrounds[i].number)
      }
      if(siteNumbers.length > 0){
        return siteNumbers
      }else{
        return 'Sorry, no campsites with that view are available to host your party'
      }
    
    

    I hope this code is what you want

    Login or Signup to reply.
    1. At the moment your code is either adding the site to the array if it’s available, or, well, "Sorry, no campsites with that view are available to host your party" just on its own should give you an error.

    2. The second point is that you appear to be calling your function from within the function.

    Here’s a rewritten example of what I think you were hoping to achieve.

    1. It assigns campgrounds[i] to cg to make the code a little easier to read.
    2. It iterates over the whole array first. If a site is reserved it skips to the next object (as you don’t need to check the rest of the condition), otherwise if it finds a match it adds that site number to the array.
    3. It returns either a) the site array if it’s not empty or b) a message that says there are no available sites.
    const campgrounds=[{number:1,view:"ocean",partySize:8,isReserved:true},{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 siteNumbers = [];
    
      for (let i = 0; i < campgrounds.length; i++) {
        
        // Make things a little more readable by assigning
        // `campgrounds[i]` to a variable
        const cg = campgrounds[i];
        
        // If the site is reserved we don't need to
        // bother checking anything else, and can
        // continue to the next object
        if (cg.isReserved) continue;
        
        // However if the site matches the
        // rest of the condition add it to the array
        if (cg.view === view && cg.partySize >= partySize) {
          siteNumbers.push(cg.number);
        }
    
      }
      
      // Once the loop is complete return either the array
      // if the array is not empty...
      if (siteNumbers.length) return siteNumbers;
      
      // ...or the "error" message
      return 'No available sites';
    
    }
    
    console.log(findMyCampsites(campgrounds, 'ocean', 38));
    console.log(findMyCampsites(campgrounds, 'forest', 4));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search