Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

I am not able to pass 4 tests. Please guide me little bit.

Your code so far

let lunches = [];

function addLunchToEnd(lunches, str) {
  let newArr = lunches.push(str);
  console.log(`${str} added to the end of the lunch menu.`);
  return lunches;
}
console.log(addLunchToEnd(lunches, "Tacos"));
console.log(addLunchToEnd(["Pizza", "Tacos"], "Burger"));


function addLunchToStart(lunches, str) {
  let newArr = lunches.unshift(str);
  console.log(`${str} added to the start of the lunch menu.`);
  return lunches;
}
console.log(addLunchToStart(["Burger", "Sushi"], "Pizza"));

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


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


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


function showLunchMenu(lunches) {
  let [lunch1, lunch2, ...rest] = lunches;
  if(lunches.length !== 0) {
    console.log(`Menu items: ${lunch1, lunch2, rest}`)
  } else if(lunches.length === 0) {
    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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi there!

Your newArr variable isn’t defined. You need to return newArr, instead of the lunches within the functions.

I don’t think there is any issue due to that, I removed the two unused newArr variable in the starting two functions. I am facing issue with the statement when array is empty. 4 tests not passing and all facing same issue. Please give me some hint.

Add the above first console logs in both function’s after the if block. Then check the editor console for errors.

The console.log statements didn’t appear.

I am going for dinner right now, when I will be back, I will revise the array lecture videos then look at my code again, There is something which I am missing.

If you still have the issues. Post your updated code here. Don’t forget to preform it using (```) back ticks on a separate line before and after your code.

  1. Move your length conditions up inside the functions so they happen before you try to use the array.

  2. You have to interpolate all three variables in the console log inside your showLunchMenu function.

Example:

const name = ["John", "Doe"];
const [first, last] = name;

console.log(`Name is: ${first, last}`); // Name is: Doe
console.log(`Name is: ${first}, ${last}`); // Name is: John, Doe
1 Like

Test passed, very precise solution. Thank you very much.

I have 1 doubts;-

  1. Why I needed to write that if statement first ?
  2. I read this one in array deconstruction but I forgot that, thank you once again :slight_smile:

Test passed, thank you very much for your help :slight_smile:

1 Like

Generally speaking, most length checks are guard checks. They should happen before you access the thing they are guarding.

In this case, without really looking at it, I would assume you are simply not logging the expected value as you have other log statements before the length check logs. So the tests sees the wrong thing being logged.

1 Like