Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

Hi! My code is not passing test 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.", but I don’t understand why?

Your code so far

let lunches = [];

function addLunchToEnd(array, string) {
  array.push(string);
  console.log(`${string} added to the end of the lunch menu.`)
  return array;
};

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


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


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


function getRandomLunch(array) {
  let arrayLength = array.length;
  if (arrayLength > 0) {
    let randomIndex = Math.round(Math.random()*arrayLength);
    console.log("Randomly selected lunch: " + array[randomIndex]);
    console.log(array[randomIndex])
  } else {
    console.log("No lunches available.");
  }
}

function showLunchMenu(array) {
  if (array.length > 0) {
    let [first, second, third] = array
    console.log("Menu items: " + first + ", " + second + ", " + third);
  } else {
    console.log("The menu is empty.");
  }
}

console.log(getRandomLunch(["Greens", "Corns", "Beans"]));


Your browser information:

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

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi @valedagradi

Try removing the second console log.

Happy coding

1 Like

if you use Math.round here, which numbers can you get?

if array length is 3, Math.random gets values from 0 to 1, so that means that you get values from 0 to 3, as in you get, 0, 1, 2, 3
so you get 4 possible numbers for 3 elements in the array

something’s wrong here