Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

SPOILERS!!!
The Code has passed but I don’t understand how some methods are executing, they’re acting differently to their defined purpose. Someone to explain to me the push(), pop(), shift(), and unshift() methods’ behavior in this code.

Your code so far

const lunches = [];

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

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

function removeLastLunch(arr){
  if(arr.push()){
    console.log(`${arr.pop()} removed from the end of the lunch menu.`);
  }else{
    console.log("No lunches to remove.");
  }
  return arr;
}
console.log(removeLastLunch(["Stew", "Soup", "Toast"]));

function removeFirstLunch(arr){
  if(arr.unshift()){
    console.log(`${arr.shift()} removed from the start of the lunch menu.`);
  }else{
    console.log("No lunches to remove.")
  }
  return arr;
};
console.log(removeFirstLunch(["Salad", "Eggs", "Cheese"]));

function getRandomLunch(arr){
  let randomLunch = Math.round(Math.random() * arr.length);
  if(arr.length === 0){
    console.log("No lunches available.");
  }else{
    console.log(`Randomly selected lunch: ${arr[randomLunch]}`);
  };
};
getRandomLunch(["Greens", "Corns", "Beans"]);

function showLunchMenu(arr){
  if(arr.length !== 0){
    console.log(`Menu items: ${arr.join(", ")}`);
  }else{
    console.log("The menu is empty.")
  }
};
showLunchMenu(["Greens", "Corns", "Beans"]);
showLunchMenu(["Pizza", "Burger", "Fries", "Salad"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:159.0) Gecko/20100101 Firefox/159.0

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

what are you confused about? the behaviour of the methods is not different from usual in this context

Here, what is the push() method doing there?

Also here, shift() and unshift() methods

Hi @Mattlight-cloud,

Please refer to this MDN reference to see what arr.push() returns. In the left-hand pane of this reference, you can also check pop(), shift(), and unshift().

Array.prototype.push() - JavaScript | MDN

Happy coding

did you wrote this code yourself?

Yes I did.

When it failed to go through with my initial code, changed those two things and it went through

can you explain where is your confusion?

each of the methods return a value, and changes something, maybe you want to look into exactly what those are?

It seemed so. pop() and shift() were being executed twice if they appeared twice in the code, and so changing the first one to an empty push() just helped to manipulate the code, same with unshift().

if an empty method is adding nothing, what value are you actually using there? what’s an empty push giving you to use?

I’m not exactly sure about that

read the documentation, test it, the answer is in your reach. Once you know what it returns you can figure out why it works as the condition and what you can use that is more idiomatic