Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

I believe I have built everything correctly, but when I try to add an item to the array only the first item is added. I think that is the only thing keeping me from passing, but if you see anything else that is wrong I would love the advise!

Your code so far

const lunches = [];

function addLunchToEnd(lunches,itemEnd){
  lunches.push(itemEnd);
  return console.log(`${itemEnd} added to the end of the lunch menu.`);
}

function addLunchToStart(lunches,itemFront){
  lunches.unshift(itemFront);
  return console.log(`${itemFront} added to the start of the lunch menu.`);
}

function removeLastLunch(lunches) {
if(lunches.length!==0){
  let removed = lunches.pop();
  return console.log(`${removed} removed from the end of the lunch menu.`);
} else {
  return console.log(`No lunches to remove.`);
}
}

function removeFirstLunch(lunches) {
if(lunches.length!==0){
  let removed = lunches.shift();
  return console.log(`${removed} removed from the start of the lunch menu.`);
} else {
  return console.log(`No lunches to remove.`);
}
}

function getRandomLunch(lunches){
  let maximum = lunches.length
  let randomNumber = Math.floor(Math.random() * (maximum + 1))
if(lunches.length!==0){
  return console.log(`Randomly selected lunch: ${lunches[randomNumber]}`);
} else {
  return console.log (`No lunches available.`);
}
}

function showLunchMenu(lunches) {
  if(lunches.legnth!==0){
    return console.log(`Menu items: ${lunches}`);
  } else {
    return console.log (`The menu is empty.`);
  }
}

addLunchToEnd(lunches, "Tacos");
addLunchToEnd(["Pizza", "Tacos"], "Burger");
removeFirstLunch(["Sushi", "Pizza", "Burger"]);

Your browser information:

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

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-lunch-picker-program/66db529d37ad966480ebb633.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @firstrainbowrose,

Try wrapping your function calls in console.log() so you can see what they are returning.

What do you see in the console when you do that?

Happy coding

Hi Dhess,

Thank you for your help! When I add a console.log() to the function calls I get this in the console:

Tacos added to the end of the lunch menu.
undefined
Burger added to the end of the lunch menu.
undefined
Sushi removed from the start of the lunch menu.
undefined

Which I think is what it was returning before adding the console.log(), just with an “undefined” added between each one.

But undefined means your function is not returning anything. Why do you think that is?

I would imagine because it is finding something additional at the end of my function, but I am not seeing anything extra for it to try and return. It is returning (some) of the right things.

I did realize I was missing lunches array in my second console.log(), so I added that. The end of the code is now:

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

Which then returns:

Tacos added to the end of the lunch menu. undefined Pizza,Tacos added to the end of the lunch menu. undefined Tacos removed from the start of the lunch menu. undefined

So I now have it so it is adding two of the three items in the second command, but it ignores the last one for some reason.

I fear it will be something easy that I am overlooking. Thank you for being patient with me while I am slow in figuring it out.

Sorry, I missed this reply! I am confused now because it is returning the message to be logged in the function, as well as undefined.

Is this a valid return? Look it up, please.

Ah ha! Well, that got rid of the undefined. I didn’t realize you couldn’t return on a console.log(). Thank you for helping me through that aspect of it.

I tested and it’s still failing me for basically everything. Hm. For example, it says “addLunchToEnd(lunches, "Tacos") should log the string "Tacos added to the end of the lunch menu." to the console.” Does that mean that I should not need to wrap it in console.log() for it to return that?

Let’s regroup. :slight_smile: Looking at the user story for addLunchToEnd(), it says you should log a message when the item is added, and you should return the updated array, right?

So, you have both a log and a return for that function.

Oh! Thank you again for being patient with me while I am struggling.

I’ve updated it so each of the functions console.log() the proper term and then returns lunches. It seems to be happier! Now to figure out why it’s not returning a random item. (I suspect in that case it’s my math. I am terrible at math… ^.^;; )

Have a look at this reference for examples of how to use Math.random():
Math.random() - JavaScript | MDN

Also, does it make sense to get a random number before you’ve determined if you have any lunches?

I thought that by generating the number I could use the variable when calling the array. In order to do that I would need to have the random number already grabbed.

I was curious and grabbed just this code to see what it returned. let maximum = lunches.length; as it turns out it returned a zero. Yet when I log just lunches it shows “[ [ ‘Pizza’, ‘Tacos’ ] ]” (which is odd that it has the brackets twice, but I will cross that bridge later.). I would imagine that should be two, yes?

How are you calling getRandomLunch()?

console.log(getRandomLunch(lunches));

I am receiving in return:
Randomly selected lunch: undefined
[ [ ‘Pizza’, ‘Tacos’ ] ]

Please post your updated code formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Does your lunches variable have anything assigned to it?

const lunches = [];


function addLunchToEnd(lunches,itemEnd){

  lunches.push(itemEnd);

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

  return lunches;

}


function addLunchToStart(lunches,itemFront){

  lunches.unshift(itemFront);

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

  return lunches;

}


function removeLastLunch(lunches) {

if(lunches.length!==0){

  let removed = lunches.pop();

  console.log(`${removed} removed from the end of the lunch menu.`);

  return lunches;

} else {

  console.log(`No lunches to remove.`);

}

}


function removeFirstLunch(lunches) {

if(lunches.length!==0){

  let removed = lunches.shift();

  console.log(`${removed} removed from the start of the lunch menu.`);

  return lunches;

} else {

  console.log(`No lunches to remove.`);

  return lunches;

}

}


function getRandomLunch(lunches){

  let maximum = lunches.length;

  let minimum = 1;

  let randomNumber = Math.floor(Math.random() * (maximum - minimum + 1) + minimum);

if(lunches.length!==0){

  console.log(`Randomly selected lunch: ${lunches[randomNumber]}`);

  return lunches;

} else {

  console.log(`No lunches available.`);

  return lunches;

}

}


function showLunchMenu(lunches) {

  if(lunches.legnth!==0){

    console.log(`Menu items: ${lunches}`);

    return lunches;

  } else {

    console.log(`The menu is empty.`);

    return lunches;

  }

}


console.log(addLunchToEnd(lunches, "Tacos"));

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

console.log(removeFirstLunch(lunches,["Sushi", "Pizza", "Burger"]));

console.log(getRandomLunch(lunches));

Theoretically it should have a few things assigned to the lunches variable. I believe it should be Pizza, Tacos, Burger, Sushi, Pizza, Burger.

Try calling it with this array of lunches since you have an error in one of your previous function calls: [ ‘Sushi’, ‘Pizza’, ‘Burger’ ], then log randomNumber inside the function. Does it ever return the first lunch item?

If I change my lunches variable to be a set array it does return a random item, and my randomNumber variable works. I can only assume that means there is something wrong with my other functions, but I am at a loss as to what it could be.