Build a Lunch Picker Program step 5

Tell us what’s happening:

I’m trying to get the list to update with [“Pizza”, “Tacos”, “Burger”] but it keeps returning [ [ ‘Pizza’, ‘Tacos’], ‘Burger’ ]

Any help would be appreciated, I’ve spend about an hour trying to figure out why it won’t log right.

Your code so far

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

console.log(lunches)



Your browser information:

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

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi there and welcome to our community!

When the addLunchToEnd function is called it should be passed the lunches array as the first argument (arr parameter):

So, you don’t need to push an array to lunches, only push the str argument to the lunches array (i.e. arr).

1 Like

when I switch it to be like that it doesn’t add anything to the array when i check the log. It shows console.log(lunches) as

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

console.log(lunches)

console log:

Pizza, Tacos,Burger, Burger added to the end of the lunch menu.
[]

Ive also tried this:

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

console.log(lunches)

with the console putting out:

Burger added to the end of the lunch menu.
[]

Your function definition can take parameters arr and str.
You should be pushing to arr inside your function, not explicitly to lunches.
However, when you call the function, you should pass lunches as the first argument, to the arr parameter.

So, not addLunchToEnd(["Pizza", " Tacos"], "Burger").

The point of this is that your function should be able to take any array which is passed to it, not just the globally-scoped lunches array which is used in this case.

1 Like

Step 5 asks us to run
addLunchToEnd(["Pizza", "Tacos"], "Burger")
and that it should return

["Pizza", "Tacos", "Burger"]

this is what I currently have

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

console.log(lunches);

but the console log only state’s that tacos have been added (adding tacos was step 4)

Tacos added to the end of the lunch menu.
Burger added to the end of the lunch menu.
[ 'Tacos' ]

I think I figured it out. im using the arr.push from the most recent post, i changed the return to “arr”
and when i run the console log like this:

console.log(addLunchToEnd(["Pizza", "Tacos"], "Burger"));

the console states:

Burger added to the end of the lunch menu.
[ 'Pizza', 'Tacos', 'Burger' ]

though when I console log the lunches variable itself there’s nothing there, is that just normal? lol I may have missed that part in the exercises/videos.

how are you doing that and how?

you mean you do not see changes when console.log(addLunchToEnd(["Pizza", "Tacos"], "Burger")); is called? there the lunches array is not used

1 Like

i was trying to console.log(lunches) without the function in the console.log instead.
so when I was trying to see the updated list with console.log(lunches) the console would show []

it all seems to be fixed now and I was able to finish the project with no further problems.