Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

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

Follow the instructions, don’t code by the test:

  1. You should create a function addLunchToEnd that takes an array as the first argument and a string as the second argument. The function should:
  • Add the string to the end of the array.
  • Log the string [Lunch Item] added to the end of the lunch menu. to the console, where [Lunch Item] is the string passed to the function.
  • Return the updated array.

Does your function do these things?

Remember to use the function parameters in the function, and not global variables like lunches

How can you confirm if this worked or not?

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’)”

so I kept changing

Yes

Yes

I don’t see where it says that in User Story 2

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.

Please review how function parameters and arguments work and make sure it’s clear
https://www.w3schools.com/js/js_function_parameters.asp
Let us know if you have any questions

ok ill take a look, sorry for the trouble and thank you!

1 Like

It’s no trouble that’s what we’re here for

Tell us what’s happening:

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

shouldn’t you pass something to the functions when calling them? you have given a parameter or two to each function

oh right dummy me, thank you so much

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.`);

return array;

}

console.log(addLunchToEnd(lunches, “Tacos”))

console.log(addLunchToEnd([“Pizza”, “Tacos”], “Burger”))

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

sorry if its a mess

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

so basically like this?

“function addLunchToEnd(lunches, str){

lunches.push(str);

console.log(`${str} added to the end of the lunch menu.`);

return lunches;

}”

you can make it less of a mess if you format the code in the post

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

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

ok ok thank you for the help!

Tell us what’s happening:

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

I merged your topics together, if you need more help with the same challenge please keep writing in the same topic

output of what function?

  1. showLunchMenu(["Greens", "Corns", "Beans"]) should log "Menu items: Greens, Corns, Beans" to the console.

Can you see the difference of your output and the expected output?

ye I noticed, thank you.

im talking about the last function “showLunchMenu”

and you are calling showLunchMenu with what array?