점심 선택기

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

Source: Dev.to

Lunch Picker 표지 이미지

점심 선택 프로그램 만들기

이 실습에서는 점심 옵션을 관리하는 프로그램을 만들게 됩니다. 점심 배열을 다루고, 배열에 항목을 추가·제거하며, 무작위로 점심 옵션을 선택하는 기능을 구현합니다.

// 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(", ")}`);
}
0 조회
Back to Blog

관련 글

더 보기 »

동기식 및 비동기식 JavaScript 배우기

Synchronous 동기식 프로그래밍에서는 작업이 순서대로 하나씩 실행됩니다. 각 코드 라인은 이전 작업이 끝날 때까지 기다렸다가 다음으로 이동합니다.

지난 몇 달 동안 Go 학습

첫 몇 주: Your Wall 찾기: Beginner Frustration Channels를 Reinforcement Tool로 활용하기 막혔나요? 여기 아직도 나를 당황하게 하는 것들 Pointers는 M에게 거짓말을 하지 않는다.