Build a Lunch Picker Program | Debugging

I am having issues with meeting the test cases for function getRandomLunch() and function showLunchMenu(). For some reason, it won’t accept the output for the test cases.

Here is my code:

const lunches = [];

function addLunchToEnd(arrVal, stringVal) {

arrVal.push(stringVal);

console.log(`${stringVal} added to the end of the lunch menu.`);

return arrVal;

}

function addLunchToStart(arrVal, stringVal) {

arrVal.unshift(stringVal);

console.log(`${stringVal} added to the start of the lunch menu.`);

return arrVal;

}

function removeLastLunch(arrVal) {

if (arrVal.length != 0) {

let removalItem = arrVal.pop();

console.log(`${removalItem} removed from the end of the lunch menu.`);



return arrVal;

}

console.log(“No lunches to remove.”);

return arrVal;

}

function removeFirstLunch(arrVal) {

if (arrVal.length != 0) {

let removalItem = arrVal.shift();

console.log(`${removalItem} removed from the start of the lunch menu.`);



return arrVal;

}

console.log(“No lunches to remove.”);

return arrVal;

}

function getRandomLunch(arrVal) {

if (arrVal.length != 0) {

const min = 0;

const max = arrVal.length - 1;



let randomVal = Math.floor(Math.random() * (max - min) + min);



console.log(`Randomly selected lunch: ${arrVal[randomVal]}`);

} else {

console.log("No lunches available.");

}

}

function showLunchMenu(arrVal) {

let […rest] = arrVal;

if (arrVal.length != 0) {

console.log(`Menu items: ${rest}`);

} else {

console.log("The menu is empty.");

}

}

// console.log(showLunchMenu([“Pizza”, “Burger”, “Fries”, “Salad”]));

Failed Test Cases:

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.

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

30. showLunchMenu(["Pizza", "Burger", "Fries", "Salad"]) should log "Menu items: Pizza, Burger, Fries, Salad" to the console.

Please post a link to this challenge.

1 Like

This is the link to the Lab/Challenge

showLunchMenu(["Pizza", "Burger", "Fries", "Salad"]);
console.log("Menu items: Pizza, Burger, Fries, Salad")

The first log is from your function call; the second is what the test says you should be logging. Do you see the difference?

This is failing because random number never picks up the last item in your array.

1 Like

Of course, that’s more-so me trying to debug and see what was being outputted to the console without adhering to the strict format.

The issue is that the ‘rest’ keyword does not include spaces between the listed items when displayed to the console. I am using the rest keyword here because I do not know how many items are in the array.

Function:

function showLunchMenu(arrVal) {

  let [...rest] = arrVal;

  if (arrVal.length != 0) {

    console.log(`Menu items: ${rest}`);

  } else {

    console.log("The menu is empty.");

  }

}

Debugging:

let tempArr = ["Pizza", "Burger", "Fries", "Salad"];

let result = showLunchMenu(tempArr);

Output:

"Menu items: Pizza,Burger,Fries,Salad"
  • No whitespace between items

Resolved function getRandomLunch() test-case error

Thank you for bringing that to my attention. I completely forgot that the range for Math.random() has the max as exclusive. So, I simply redacted the offset.

function getRandomLunch(arrVal) {

  if (arrVal.length != 0) {

    const min = 0;

    const max = arrVal.length;




    let randomVal = Math.floor(Math.random() * (max - min) + min);




    console.log(`Randomly selected lunch: ${arrVal[randomVal]}`);

  } else {

    console.log("No lunches available.");

  }

}

That test case error is now fixed.

The remaining test case errors are for the function showLunchMenu().

29. showLunchMenu(["Greens", "Corns", "Beans"]) should log "Menu items: Greens, Corns, Beans" to the console.
30. showLunchMenu(["Pizza", "Burger", "Fries", "Salad"]) should log "Menu items: Pizza, Burger, Fries, Salad" to the console.

Nevermind, I fixed it.

function showLunchMenu(arrVal) {

  let [...rest] = arrVal;

  let tempArr = rest;

  tempArr = tempArr.join(", ");



  if (arrVal.length != 0) {

    console.log("Menu items: " + tempArr);

  } else {

    console.log("The menu is empty.");

  }

}

good job in fixing the function, but do you need all these?
let [...rest] = arrVal;, arrVal is already an array. Did you want to make a copy? this function should not change the array at all so it’s not necessary

let tempArr = rest; here you have the same array but changing the variable name

1 Like

Yeah, after re-reading my code in this section I realize there is some redundancy. I was '“hacking” away at possible solutions and wasn’t being efficient. I believe below would be a simpler alternative:


const arrString = arrVal.join(", ");

if (arrVal.length != 0) {

    console.log("Menu items: " + arrString);

  } else {

    console.log("The menu is empty.");

  }

Since the .join() method doesn’t directly alter the original array. Unless I am wrong. I believe that is why I created a copy in that moment, as I think back to my original thought process.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.