Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

Tests 29 and 30 won’t complete and I need assistance on the right solution. Any help is much appreciated.

Your code so far

const lunches = [];

const addLunchToEnd = (arr, str) => {
  arr.push(str);
  console.log(`${str} added to the end of the lunch menu.`);
  return arr;
};

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

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

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

const getRandomLunch = arr => {
  if (arr.length == 0) {
    console.log('No lunches available.');
  } else {
    let ranLunchNum = Math.floor(Math.random() * 3);
    let ranLunch = arr[ranLunchNum];
    console.log(`Randomly selected lunch: ${ranLunch}`);
  }
}

const showLunchMenu = arr => {
  if (lunches.length != 0) {
    console.log(`Menu items:${arr}`)
  } else {
    console.log("The menu is empty.");
  }
}


Your browser information:

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

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program
https://www.freecodecamp.org/learn/full-stack-developer/lab-lunch-picker-program/build-a-lunch-picker-program

Have you tried what your function does?

Because for

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

I tried to write showLunchMenu(["Greens", "Corns", "Beans"]) in the editor and what I see in the console is The menu is empty., which does not seem correct, the menu is not empty

Hi @The-Country-Coder

The logic is check if length of lunches array is not empty.

If there are items, console log the array passed to the function.

Otherwise, console log the menus is empty.

Since the lunches array starts with no items, that condition will always eventuate.

How could you change that behaviour?

Happy coding

I figured out my bug. If you look at my previous code, note that in my first if statement, I have it written as:

const showLunchMenu = arr => {
  if (lunches.length != 0) {
    console.log(`Menu items: ${arr.join(', ')}`)

I was checking the length of the lunches parameter. The thing is I don’t have a lunches parameter, it’s written as arr. So in my new code down below, I have replaced the lunches with arr. I ran the tests and it works! Thanks @Teller for the hint!

const showLunchMenu = arr => {
  if (arr.length != 0) {
    console.log(`Menu items: ${arr.join(', ')}`)
  } else {
    console.log("The menu is empty.");
  }
}

console.log(showLunchMenu(["Greens", "Corns", "Beans"]))
1 Like