Build a Lunch Picker Program - Build a Lunch Picker Program

Tell us what’s happening:

Hi, the test 28 is not passed. why the out put is true

Your code so far

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


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


function removeLastLunch(lunches){
 
  if(lunches.length!==0){
        let popedLunch= lunches.pop();
console.log(`${popedLunch} removed from the end of the lunch menu.`);
  }
  else{
  console.log("No lunches to remove."); 
  }
    return lunches;
}
removeLastLunch(["Stew", "Soup", "Toast"]);


function removeFirstLunch(lunches){
  if(lunches.length!=0){
  let removedItem=lunches.shift();
  console.log(`${removedItem} removed from the start of the lunch menu.`)
  
  }
else{
    console.log("No lunches to remove.");
  }
 return lunches;
}
removeFirstLunch(["Salad", "Eggs", "Cheese"]);

//get Random Lunch function:
function getRandomLunch(lunches){
  if(lunches.length==0){
    console.log('No lunches available.');
  }
  else{
      let randNumber=Math.floor(Math.random() *3);
  let randElement=lunches[randNumber];
    console.log(`Randomly selected lunch: ${randElement}`); 
  }
}
getRandomLunch(["Salad", "Eggs", "Cheese"]);


// funation show Lunch Menu
function showLunchMenu(lunches){
  
    if(lunches.length!=0){
      console.log(`Menu items:${lunches}`);
  }
  else{
      console.log('The menu is empty.');
  }
}
//showLunchMenu([]);
showLunchMenu(["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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Lunch Picker Program - Build a Lunch Picker Program

while the out put is true

Please show the output of your function and what the test says the output should be?

// running tests 28.

showLunchMenu(["Greens", "Corns", "Beans"])

should log

"Menu items: Greens, Corns, Beans"

to the console. // tests completed // console output Pizza,Tacos,Burger added to the end of the lunch menu. Burger,Sushi,Pizza added to the start of the lunch menu. Toast removed from the end of the lunch menu. Salad removed from the start of the lunch menu. Randomly selected lunch: Cheese Menu items:Greens,Corns,Beans

This is what is asked to be logged, right? Don’t you think there is some difference with your log of "Menu items:Greens,Corns,Beans"?

I made some changes but not passed
function showLunchMenu(lunches){
let [lunch1, lunch2, …rest] = lunches

if(lunches.length!=0){
  console.log(`Menu items:${ lunch1},${lunch2},${rest}`);

}

Please share the output of your function, show what it actually logs to the console.

@ILM has shared it already:

You can see it’s slightly different spacing than what it should log:

Do you see the difference?

1 Like

yes it passed , the last edited form of showLunchMenu function is as follow :slight_smile:

// funation show Lunch Menu
function showLunchMenu(lunches){
    let [l1,l2,l3,...rest] = lunches
  
    if(lunches.length!=0){
      console.log(`Menu items: ${l1}, ${l2}, ${l3}`);
  }
  else{
      console.log('The menu is empty.');
  }
}
showLunchMenu(["Greens", "Corns", "Beans"]);

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

what if there aren’t three items, what if there are 2, or 7?

function showLunchMenu(lunches){
    let [l1,l2,...rest] = lunches
  
    if(lunches.length!=0){
      console.log(`Menu items: ${l1}, ${l2}, ${rest}`);
  }
  else{
      console.log('The menu is empty.');
  }
}
showLunchMenu(["Greens", "Corns", "Beans"]);```

if there were mor elements we will use rest operator as shown in the code.

and will that show the items as comma separated?

1 Like
function showLunchMenu(lunches) {
    if (lunches.length != 0) {
        console.log(`Menu items: ${lunches.join(', ')}`);
    }
    else {
        console.log('The menu is empty.');
    }
}
showLunchMenu(["Greens", "Corns", "Beans", "tea", "Beans", "tea"]);

is it true ?

1 Like

the output seems good, did you test it?

Yes it worked, if you suggest another technique please share it.

I dont understand this, can someone please explain

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.