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