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
Your logic seems to be wrong before the readability issue.
will be..
I hope this code is what you want
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.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.
campgrounds[i]
tocg
to make the code a little easier to read.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.