Tell us what’s happening:
Why removeLastLunch is not working when it sets out the requirement?
namely
removeLastLunch(["Stew", "Soup", "Toast"]) should log the string "Toast removed from the end of the lunch menu." to the console
then
removeLastLunch(["Sushi", "Pizza", "Noodles"]) should return ["Sushi", "Pizza"].
Your code so far
const lunches = [];
function addLunchToEnd(array, string){
lunches.push(array);
let newArr = []
if(array.length == 0){
console.log(`${string} added to the end of the lunch menu.`)
}else{
array.map(item => {
newArr.push(item)
})
newArr.push(string)
console.log(newArr)
}
}
addLunchToEnd(["Pizza", "Tacos"], "Burger")
function addLunchToStart(array, string){
array[0] = string;
console.log(`${string} added to the start of the lunch menu.`);
}
function removeLastLunch(array){
if(array.length === 0){
console.log("No lunches to remove.")
}else if(array[array.length - 1] === 'Toast'){
console.log(`${array[array.length - 1]} removed from the end of the lunch menu.`)
}else{
const newArray = []
for(let i = 0; i < array.length - 1; i++){
newArray.push(array[i]);
}
console.log(newArray);
}
}
removeLastLunch(["Sushi", "Pizza", "Noodles"])
function getRandomLunch(arr){
const random = Math.floor(Math.random() * arr.length);
if(random.length >= 0){
console.log(`Randomly selected lunch ${random}`)
}else{
console.log(`No lunches available.`)
}
}
function showLunchMenu(array){
if(array.length > 0){
console.log(`Menu items: ${array.join(", ")}`)
}else{
console.log('The menu is empty.')
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Challenge Information:
Build a Lunch Picker Program - Build a Lunch Picker Program
ILM
November 24, 2025, 12:39pm
2
what is random? what is random.length?
Oh this was not the issue, it is suppose to be deleted. The correct code that I am running is
function removeLastLunch(array){
if(array.length === 0){
console.log("No lunches to remove.")
}else if(array[array.length - 1] === 'Toast'){
console.log(`${array[array.length - 1]} removed from the end of the lunch menu.`)
}else{
const newArray = []
for(let i = 0; i < array.length - 1; i++){
newArray.push(array[i]);
}
console.log(newArray);
}
}
ILM
November 24, 2025, 1:30pm
4
why are you hardcoding Toast? you should not hardcode
what are you doing with newArray?
also make sure you are doing all the things needed for this function
For the same reason I hardcoded Toast, otherwise what is the difference between testing
removeLastLunch(["Stew", "Soup", "Toast"])
and
removeLastLunch(["Sushi", "Pizza", "Noodles"])
because both have strings in them
Edit: I removed the hardcoded Toast but how can I make difference between the user story that doesn’t have Toast and have same amount of string array?
This is the latest iteration of my code
function removeLastLunch(array){
const popped = array.pop();
if(array.length === 0){
console.log("No lunches to remove.")
}else{
console.log(`${popped} removed from the end of the lunch menu.`)
}
}
Now the problem is finding a condition to print the array which is now modified. I am gonna think over the condition or clue to move forward until I see you reply.
ILM
November 24, 2025, 3:09pm
7
are you sure you are asked to print the updated array?
I haven’t printed the updated array, the problem or struggle I am facing is to put that updated condition in what so it prints out the updated array.
ILM
November 24, 2025, 6:02pm
9
you do not need to print the updated array, you need to return it
The user srtories I understood says
If the array is empty then it should print out a string telling No lunches to remove.
Print the last element in the array
Print the modified array
The first 2 I have done with a condition, it is if or else but if I put third one outside of if and else I will print out either one of the conditon.
ILM
November 24, 2025, 6:39pm
11
mahassan:
Print the modified array
this is not what’s written in the user stories, read the user stories again
You should create a function removeLastLunch that takes an array as its argument. The function should:
Remove the last element from the array.
If the removal is successful, log the string [Lunch Item] removed from the end of the lunch menu. to the console, where [Lunch Item] is the element removed from the array.
If the array is empty, log the string "No lunches to remove." to the console.
Return the updated array.
that is what I wrote in my own words.
Now my code is
function removeLastLunch(array){
if(array.length === 0){
console.log("No lunches to remove.")
}else{
const popped = array.pop()
console.log(`${popped} removed from the end of the lunch menu.`)
}
console.log(array)
}
This is what I have now. I am not sure what I am missing because the user stories are not passing.
Oh I was able to do it. The only thing I had to change is return
function removeLastLunch(array){
if(array.length === 0){
console.log("No lunches to remove.")
}
const popped = array.pop()
console.log(`${popped} removed from the end of the lunch menu.`)
return array;
}
ILM
November 24, 2025, 8:05pm
14
yeah, because printing is not the same as returning
True. I didn’t read the word return and was fixed on logging. Spend whole day on this. Thanks for help and support!