Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

I need help with the show menu function.

Error messages:

  1. When the input array is empty, the function showLunchMenu should log the string “The menu is empty.” to the console.
  2. showLunchMenu([“Greens”, “Corns”, “Beans”]) should log “Menu items: Greens, Corns, Beans” to the console.
  3. showLunchMenu([“Pizza”, “Burger”, “Fries”, “Salad”]) should log “Menu items: Pizza, Burger, Fries, Salad” to the console.
    // tests completed

Your code so far

let lunches = [];
// add lunch item to end of lunch menu
function addLunchToEnd(lunches,str){
  lunches.push(str);
  console.log(str + " added to the end of the lunch menu.");
  return lunches
}

console.log(addLunchToEnd(lunches,"Tacos"));

//-------------------------------------------
// add to front of lunch menu
function addLunchToStart(lunches,str){
  lunches.unshift(str);
  console.log(str + " added to the start of the lunch menu.");
  return lunches
}
console.log(addLunchToStart(lunches,"Sushi"))

//-------------------------------------------
// remove last lunch item from lunch menu
function removeLastLunch(lunches){
  if (lunches.length === 0) {
    console.log("No lunches to remove.");
  }
 let lastArrEl = lunches.pop();
  console.log(lastArrEl + " removed from the end of the lunch menu.");
  return lunches
}
lunches = ["Sushi","Pizza","Noodles"]
console.log(removeLastLunch(lunches));
lunches = ["Stew","Soup","Toast"]
console.log(removeLastLunch(lunches));


//--------------------------------------------
// remove first lunch item
function removeFirstLunch(lunches){
  
  if(lunches.length === 0){
    console.log("No lunches to remove.")
    return lunches
  }else{
  
  let removeFirst = lunches.shift()
  console.log(removeFirst + " removed from the start of the lunch menu.");
  return lunches
}
}
console.log(removeFirstLunch(["Salad","Eggs","cheese"]))

//---------------------------------------

// get random lunch item
function getRandomLunch(lunches){
  if(lunches.length === 0){
    console.log("No lunches available.");
    return lunches
  }else {
    let randomIndex = Math.round(Math.random() * (lunches.length - 0) + 0)
  let lunch = lunches[randomIndex];
      console.log("Randomly selected lunch: " + lunch)
      return lunches
  }
}
console.log(getRandomLunch(lunches));
//-----------------------------------
// show lunch menu  
/*function showLunchMenu(lunches){
  if (lunches.length > 0){
    let [first,second,third] = lunches;
    console.log(`Menu items: ${first},${second},${third}`)
  }else console.log("The menu is empty.")
}
*/
function showLunchMenu(lunches){
  if(lunches.length > 0){
    let [first,second, third] = lunches
    console.log("Menu items: " + first +"," + second + "," + third)
    return lunches
    }
    else{
      console.log("The menu is empty");
      return lunches
    }
}
let lunch = ["Greens", "Beans", "Corn"];
console.log(showLunchMenu(lunch))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi @dreyes61

  1. Compare the expected output to your output.
  2. Looks like you are also outputting the array. The problem though is with the spacing of the items.
let lunch = ["Greens", "Beans", "Corn"];
console.log(showLunchMenu(lunch));

// Menu items: Greens,Beans,Corn
// [ 'Greens', 'Beans', 'Corn' ]
  1. Where is the apple?
console.log(showLunchMenu(['pizza', 'toast', 'lime', 'apple']),);

// Menu items: pizza,toast,lime
// [ 'pizza', 'toast', 'lime', 'apple' ]

Happy coding

Tell us what’s happening:

I’ve been trying to correctly answer the final two tests for a few days now,
If i remove the return lunches statement, i get undefined after the menu items are logged.
Please help, Thanks

Your code so far

// declare lunches variable
let lunches = [];
// addLunchToEnd function
function addLunchToEnd(lunches,str){
  lunches.push(str)
  console.log(str + " added to the end of the lunch menu.");
return lunches
}
console.log(addLunchToEnd(lunches,"Tacos"))
  
//addToStart function
function addLunchToStart(lunches,str){
  lunches.unshift(str);
  console.log(str + " added to the start of the lunch menu.")
return lunches
}

console.log(addLunchToStart(lunches,"Sushi"));

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

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

//get randomLunch function
function getRandomLunch(lunches){
  if(lunches.length > 0) {
  let rndmNum = Math.round(Math.random() * lunches.length);
  let rndmLunch = lunches[rndmNum]
  console.log("Randomly selected lunch: " + rndmLunch);
  }
  if(lunches.length === 0){
    console.log("No lunches available.");
  }
  return lunches
}
console.log(getRandomLunch(lunches))

//showLunchMenu 

function showLunchMenu(lunches){
  if(lunches.length === 0){
    console.log("The menu is empty.")
  }
  if(lunches.length > 0){
    let [first,second,third] = lunches;
    console.log(`Menu items:  ${first} ${second} ${third}` + " " + lunches[lunches.length - 1]);
  }
  
 return lunches
}
console.log(showLunchMenu(["Greens","Corns","Beans"]));
console.log(showLunchMenu(["Pizza","Burger","Fries","Salad"])); 


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi.

On your final function:

  1. The first if statement is correct.
  2. An else statement will suffice for the remaining code.
  3. You are not asked to return anything, you are logging to the console.
  4. Your code needs to work with any number of items in the array passed to the function. Look at what you are asked to do. You are asked to print the lunch items, separated by a comma and a space. Can you remember which method to achieve that?

Tell us what’s happening:

No matter what I try, I can’t solve the last two Tests, please help,
Everything i try fails.

Your code so far

// declare lunches variable
let lunches = [];
// addLunchToEnd function
function addLunchToEnd(lunches,str){
  lunches.push(str)
  console.log(str + " added to the end of the lunch menu.");
return lunches
}
console.log(addLunchToEnd(lunches,"Tacos"))
  
//addToStart function
function addLunchToStart(lunches,str){
  lunches.unshift(str);
  console.log(str + " added to the start of the lunch menu.")
return lunches
}

console.log(addLunchToStart(lunches,"Sushi"));

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

function removeFirstLunch(lunches){
  let shifted = lunches.shift();
  if(lunches.length === 0){
    console.log("No lunches to remove.");
  }
  console.log(shifted + " removed from the start of the lunch menu.");
  return lunches
  
}
console.log(removeFirstLunch(lunches));
console.log("Sushi","Pizza","Burger");
//get randomLunch function
function getRandomLunch(lunches){
  if(lunches.length > 0) {
  let rndmNum = Math.round(Math.random() * lunches.length);
  let rndmLunch = lunches[rndmNum]
  console.log("Randomly selected lunch: " + rndmLunch);
  
  }
  if(lunches.length === 0){
    console.log("No lunches available.");
  }
  return lunches
}
console.log(getRandomLunch(lunches));


//showLunchMenu 
function showLunchMenu(lunches){
  if(lunches.length > 0){
    console.log("Menu items: " + `${lunches.toString()}`)
  }
  
  if(lunches.length === 0){
    console.log("The menu is empty.");

  }
  return lunches
}
console.log(showLunchMenu(lunches))
console.log(showLunchMenu(["Greens","Corns","Beans"]))
console.log(showLunchMenu(["Pizza","Burgers","Fries","Salad"]))
 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

Hi @dreyes61

Compare the expected output to the output of your function.

  1. a spacing issue
  2. an array which is not required.

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Thanks.

I see the join method and passed the las 2 tests. Thanks for the hint.

1 Like