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
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.
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.’.
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