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
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)
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.
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' ]
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.
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.