Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

Hello, the following logic passes the removeLastLunch step, but when using the function to remove “Toast” from [“Stew”, “Soup”, “Toast”], the console logs…

“Stew,Soup,Toast removed from the end of the lunch”.

Have I made a mistake somewhere?

Your code so far

function removeLastLunch (arr) {
  if (arr.length === 0) {
    console.log("No lunches to remove.")
  } else {
    let removedEnd = arr.pop();
    console.log(`${removedEnd} removed from the end of the lunch menu.`);
  } return arr;
} //incorrect logic?

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

I do not see that:

are you sure the code you have shared is the one giving the issue?

Sorry for the late response.

The code shared is the one that gives the issue; it passes, but the console logs a different message than the one it’s supposed to. I have no idea why this happened, but I’m glad the logic used is correct.

1 Like

can you share all your code then please? I do not see at all what you see

I’m starting to think the problem is somewhere else:

const lunches = []

function addLunchToStart (arr, str) {
  arr.unshift(str);
  console.log(`${str} added to the start of the lunch menu.`);
  return arr;
}

function removeLastLunch (arr) {
  if (arr.length === 0) {
    console.log("No lunches to remove.")
  } else {
    let removedEnd = arr.pop();
    console.log(`${removedEnd} removed from the end of the lunch menu.`);
  } return arr;
} 


addLunchToStart(lunches, ["Stew", "Soup", "Toast"]);
removeLastLunch(lunches)

Calling removeLastLunch here returns ‘Stew,Soup,Toast removed from the end of the lunch menu.’.

here you are adding one array to lunches, not one string, that means that ["Stew", "Soup", "Toast"] will be one element inside lunches

removeLastLunch is doing nothing wrong

So if I got this right, it’d be best to remove the lunches array altogether, and log in whatever array I’d like to test? None of my logic uses the lunches array as it stands, it’s there as a placeholder.

As a matter of fact, calling…
removeLastLunch(["Stew", "Soup", "Toast"])
returns…
'Toast removed from the end of the lunch menu.'
…as intended.

you can work with the lunches array, but you need to review how you are using addLunchToEnd

if you write addLunchToStart(lunches, ["Stew", "Soup", "Toast"]); the lunch you are adding is the whole array

if you want the three strings to be separate lunches you need to add them separately, first addLunchToStart(lunches, "Stew") and then the others one by one

It all checks out now. Thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.