Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

Hi!! I cannot pass test 23 and 24:
23. When the input array is empty, the function getRandomLunch should log the string “No lunches available.” to the console.
24. When the input array is not empty, the function getRandomLunch should log a string in the format Randomly selected lunch: [Lunch Item] to the console.

Idk what to put as the return statement. I have tried to put the array as the return but it still doesn’t pass

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;
};

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

console.log(addLunchToEnd(lunches, "Salad"));
console.log(addLunchToEnd(lunches, "Eggs") );
console.log(addLunchToEnd(lunches, "Cheese"));

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


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


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



console.log(removeFirstLunch(lunches));



console.log(getRandomLunch(lunches));














Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Are you using this argument correctly?

yes. It works for all the other functions. When i change it to lunches the result is still the same

You never use arr in your function EDIT(well you access arr.length but that’s all)

I’m not worried about the other functions. I’m worried about that one function though. arr should be used consistently inside of your function and not any other arrays.

Global variable use spoils function reusability here.

yes thanks! good spot. I switched lunches[randomLunch] to arr and then it worked :slight_smile: + returned the array

1 Like