점심 선택기
발행: (2026년 3월 26일 AM 09:22 GMT+9)
2 분 소요
원문: Dev.to
Source: Dev.to

점심 선택 프로그램 만들기
이 실습에서는 점심 옵션을 관리하는 프로그램을 만들게 됩니다. 점심 배열을 다루고, 배열에 항목을 추가·제거하며, 무작위로 점심 옵션을 선택하는 기능을 구현합니다.
// create an empty array
let lunches = [];
// add a lunch to the end of the array
function addLunchToEnd(arr, lunchItem) {
arr.push(lunchItem);
console.log(`${lunchItem} added to the end of the lunch menu.`);
return arr;
}
// add a lunch to the beginning of the array
function addLunchToStart(arr, lunchItem) {
arr.unshift(lunchItem);
console.log(`${lunchItem} added to the beginning of the lunch menu.`);
return arr;
}
// remove the last lunch item
function removeLastLunch(arr) {
if (arr.length === 0) {
console.log("No lunches to remove.");
return arr;
}
const removed = arr.pop();
console.log(`${removed} removed from the end of the lunch menu.`);
return arr;
}
// remove the first lunch item
function removeFirstLunch(arr) {
if (arr.length === 0) {
console.log("No lunches to remove.");
return arr;
}
const removed = arr.shift();
console.log(`${removed} removed from the start of the lunch menu.`);
return arr;
}
// get a random lunch item
function getRandomLunch(arr) {
if (arr.length === 0) {
console.log("No lunches available.");
return;
}
const randomIndex = Math.floor(Math.random() * arr.length);
const item = arr[randomIndex];
console.log(`Randomly selected lunch: ${item}`);
}
// show the current lunch menu
function showLunchMenu(arr) {
if (arr.length === 0) {
console.log("The menu is empty.");
return;
}
console.log(`Menu items: ${arr.join(", ")}`);
}