Im having trouble in exercise 5, I don’t know what’s happening, when I try to log the pizza in the start it doesn’t work, the output is “,Tacos,Burguer” the pizza doesn’t appear at all can someone help me please?
Your code so far
let lunches = [];
function addLunchToEnd(array, str){
array = lunches.push(str);
console.log(`${str} added to the end of the lunch menu.`);
return array;
}
console.log(addLunchToEnd(lunches, "Tacos"));
console.log(addLunchToEnd(["Pizza", "Tacos"], "Burger"));
function addLunchToStart(array, str){
array = lunches.unshift(str);
console.log(`${str} added to the start of the lunch menu.`);
return array;
}
function removeLastLunch(array){
if(array != null){
array = lunches.pop();
console.log(`${array} removed from the end of the lunch menu.`)
return array;}
else if(array == 0){
console.log("No lunches remove.")
return array;
}
}
function removeFirstLunch(array){
if(array != null){
array = lunches.shift();
console.log(`${array} removed from the start of the lunch menu.`)
return array;}
else if(array == null){
console.log("No lunches to remove.")
return array;
}
}
function getRandomLunch(array) {
let randomNum = Math.floor(Math.random() * lunches.length);
let randomElement = lunches[randomNum];
console.log(`Randomly selected lunch: ${randomElement}`);
if(randomNum === 0) {
console.log("No lunches available.")
}
}
function showLunchMenu(array){
array = lunches
if(lunches.length !== 0){
console.log(`Menu items: ${array}`)
} else if (lunches.length === 0){
console.log("The menu is empty.")
}
}
addLunchToEnd();
addLunchToStart();
removeLastLunch();
removeFirstLunch();
getRandomLunch();
showLunchMenu();
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3 Safari/605.1.15
Challenge Information:
Build a Lunch Picker Program - Build a Lunch Picker Program
doesn’t the first topic ask to create a var named lunch and assign it an empty string?
“You should declare a variable lunches and assign it an empty array to store the lunch items.”
doesn’t this mean a global var? and then use it on the functions
about the last part, I first did like this “function addLunchToEnd(lunches, str){
lunches.push(str);
console.log(`${str} added to the end of the lunch menu.`);
return lunches;
}”
but the output is this “TypeError: undefined is not an object (evaluating ‘lunches.push’)”
This certainly refers to the array and the string parameters of the function. If you pass the lunches array to the function, then it would be the array.
Why is my output in the end this “Menu items: ,Tacos, “ if I don’t have any items before Tacos, why does it have a comma in the start and in the end, shouldn’t it be “Menu items: Tacos”
this is the full output:
Tacos added to the end of the lunch menu.
1
undefined added to the end of the lunch menu.
undefined added to the start of the lunch menu.
No lunches to remove.
Randomly selected lunch: Tacos
Menu items: ,Tacos,
Thank you!
Your code so far
let lunches = [];
function addLunchToEnd(array, str){
array = lunches.push(str);
console.log(`${str} added to the end of the lunch menu.`);
return array;
}
console.log(addLunchToEnd(lunches, "Tacos"))
function addLunchToStart(array, str){
array = lunches.unshift(str);
console.log(`${str} added to the start of the lunch menu.`);
return array;
}
function removeLastLunch(array){
if(array != null){
array = lunches.pop();
console.log(`${array} removed from the end of the lunch menu.`)
return array;}
else if(array == 0){
console.log("No lunches remove.")
return array;
}
}
function removeFirstLunch(array){
if(array != null){
array = lunches.shift();
console.log(`${array} removed from the start of the lunch menu.`)
return array;}
else if(array == null){
console.log("No lunches to remove.")
return array;
}
}
function getRandomLunch(array) {
let randomNum = Math.floor(Math.random() * lunches.length);
let randomElement = lunches[randomNum];
console.log(`Randomly selected lunch: ${randomElement}`);
if(randomNum === 0) {
console.log("No lunches available.")
}
}
function showLunchMenu(array){
array = lunches
if(lunches.length > 0){
console.log(`Menu items: ${array}`)
} else if (lunches.length < 0){
console.log("The menu is empty.")
}
}
addLunchToEnd();
addLunchToStart();
removeLastLunch();
removeFirstLunch();
getRandomLunch();
showLunchMenu();
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3 Safari/605.1.15
Challenge Information:
Build a Lunch Picker Program - Build a Lunch Picker Program
another question, im having trouble in exercise 5, it asks this “addLunchToEnd(["Pizza", "Tacos"], "Burger")should return ["Pizza", "Tacos", "Burger"].”
I have this “function addLunchToEnd(array, str){
array = lunches.push(str);
console.log(`${str} added to the end of the lunch menu.`);
and it only gives me this “Menu items: Tacos,Burger”
”Tacos added to the end of the lunch menu.
1
Burger added to the end of the lunch menu.
2
undefined added to the end of the lunch menu."
now that you are calling the function with arguments, , don’t you think that you should use the arguments? instead of using a global variable like lunches?
when you call addLunchToEnd(["Pizza", "Tacos"], "Burger"), ["Pizza", "Tacos"] is not lunches
having a function parameter and a global variable having the same name can be confusing, if you think it’s not confusing for you, then you can use any name for the parameters
Im having trouble in finishing exercise 29 and 30, and why is my output being repeat a few times, this is my output `Menu items: Burger,Sushi,Pizza,Pizza,Tacos,Burger`
Thank you!
Your code so far
let lunches = []
function addLunchToEnd(lunches, str){
lunches.push(str);
console.log(`${str} added to the end of the lunch menu.`);
return lunches;
}
addLunchToEnd(lunches,["Pizza", "Tacos", "Burger"]);
function addLunchToStart(lunches, str){
lunches.unshift(str);
console.log(`${str} added to the start of the lunch menu.`);
return lunches;
}
addLunchToStart(lunches,["Burger", "Sushi", "Pizza"]);
function removeLastLunch(lunches){
if(lunches.length!==0){
let popedLunch= lunches.pop();
console.log(`${popedLunch} removed from the end of the lunch menu.`);
}
else{
console.log("No lunches to remove.");
}
return lunches;
}
removeLastLunch(["Stew", "Soup", "Toast"]);
function removeFirstLunch(lunches){
if(lunches.length!=0){
let removedItem=lunches.shift();
console.log(`${removedItem} removed from the start of the lunch menu.`)
}
else{
console.log("No lunches to remove.");
}
return lunches;
}
removeFirstLunch(["Salad", "Eggs", "Cheese"]);
function getRandomLunch(lunches){
if(lunches.length == 0){
console.log('No lunches available.');
}
else{
let randNumber = Math.floor(Math.random() *3);
let randElement = lunches[randNumber];
console.log(`Randomly selected lunch: ${randElement}`);
}
}
getRandomLunch(["Salad", "Eggs", "Cheese"]);
function showLunchMenu(lunches){
lunches
if(lunches.length > 0){
console.log(`Menu items: ${lunches}`)
} else{
console.log("The menu is empty.")
}
}
showLunchMenu(["Greens", "Corns", "Beans"]);
addLunchToEnd(lunches);
addLunchToStart(lunches);
removeLastLunch(lunches);
removeFirstLunch(lunches);
getRandomLunch(lunches);
showLunchMenu(lunches);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3 Safari/605.1.15
Challenge Information:
Build a Lunch Picker Program - Build a Lunch Picker Program