Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

couldn’t pass 28 and 29 although the output seems right.

Your code so far

let lunches = [];

function addLunchToEnd(lunches,y){
  lunches.push(y);
  console.log(`${y} added to the end of the lunch menu.`)
  return lunches;
}
console.log(addLunchToEnd(['Pizza','Burger'], "Tacos"));

function addLunchToStart(lunches,x){
lunches.unshift(x);
console.log(`${x} added to the start of the lunch menu.`)
return lunches;
}
console.log(addLunchToStart(['Pizza','Burger'],"Sushi"))

function removeLastLunch(lunches){
  if (lunches.length>0){
  let removedItemLast = lunches.pop();
  console.log(`${removedItemLast} removed from the end of the lunch menu.`)
  }else if (lunches.length==0){
    console.log(`No lunches to remove.`)
    }
  return lunches;
}
console.log(removeLastLunch(['Pizza','Burger','Soup']));

function removeFirstLunch(lunches){
  if (lunches.length>0){
  let removedItemFirst = lunches.shift();
  console.log(`${removedItemFirst} removed from the start of the lunch menu.`)
  }else if (lunches.length==0){
    console.log(`No lunches to remove.`)
    }
  return lunches;
}
console.log(removeFirstLunch(['Pizza','Burger','Soup']));

function getRandomLunch(lunches){
  if (lunches.length>0){
   lunches = lunches[Math.floor(Math.random()*lunches.length)];
   console.log(`Randomly selected lunch: ${lunches}`);
  } else if(lunches==0){
    console.log(`No lunches available.`);
  }
}
getRandomLunch(['Pizza','Burger','Sushi']);

function showLunchMenu(lunches){
  if (lunches.length>0){
    console.log(`Menu items: ${lunches}`)
  } else if (lunches==0){
    console.log(`The menu is empty.`);
  }
}
showLunchMenu(["Greens", " Corns", " Beans"]);
showLunchMenu(["Pizza", " Burger", " Fries", " Salad"]);




Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi.

 console.log(`Menu items: ${lunches}`)

The console is misleading here. There is a method to return an array as a string. Do you recall what it is? Take note from the tests as to how the string should appear.

If you can find that, it should pass.

1. In your getRandomLunch function:

  • When you’re checking if the lunches array is empty:
    • Change this: The line that says else if(lunches==0) isn’t the right way to check if an array is empty. You need to check its length.
    • Do this instead: Change that condition to something like else if (lunches.length === 0).
  • When you’re picking a random lunch:
    • Change this: Right now, your line lunches = lunches[Math.floor(Math.random()*lunches.length)]; changes the lunches variable itself to become the single chosen food item.
    • Do this instead: Create a new variable (maybe call it let chosenLunch) and store the randomly selected item in that new variable. Then, in your console.log, print this chosenLunch variable. Leave the lunches variable that was passed into the function as an array.

2. In your showLunchMenu function:

  • When you’re checking if the lunches array is empty:
    • Change this: Same issue as above, the line with else if (lunches==0) needs an update.
    • Do this instead: Change the condition to check if the array’s length is zero (e.g., lunches.length === 0).
  • When you’re printing the menu items (this is key for tests 28 & 29):
    • Change this: Your current console.log(\Menu items: ${lunches}`)` just puts a comma between items. The tests want a comma and a space.
    • Do this instead: You need to use the join() method on your lunches array to get the formatting right. Change it so it looks something like this inside your console.log: lunches.join(', '). The whole line would be something like console.log(\Menu items: ${lunches.join(', ')}`);`.

3. (Optional, but cleans up the code a bit) In your removeLastLunch and removeFirstLunch functions:

  • Change this: After your if (lunches.length > 0) condition, you have else if (lunches.length == 0).
  • Do this instead (optional): You can simplify that else if to just else. If the array’s length isn’t greater than zero, it must be zero, so else covers it.

Try making these updates, and see how the tests go.

1 Like

Thank you for the guidance.