Menu
Coddy logo textTech

전체 재고 가치 계산하기

Coddy Lua 여정의 Fundamentals 섹션에 포함된 레슨 — 90개 중 80번째.

challenge icon

챌린지

쉬움

이제 다음 인벤토리를 사용하세요 (현재 인벤토리를 덮어써도 됩니다):

local inventory = {
   {name = "Health Potion", price = 15, stock = 8},
   {name = "Mana Potion", price = 12, stock = 5},
   {name = "Strength Potion", price = 25, stock = 3},
   {name = "Speed Potion", price = 18, stock = 2}
}


여러분의 목표는 재고가 있는 모든 포션의 총 골드 가치를 찾는 것입니다.

단계별 수행 방법은 다음과 같습니다:

1. totalValue라는 변수를 생성하고 0으로 설정하세요. 이 변수는 각 포션을 확인할 때마다 합계를 추적합니다.

2. 숫자형 for 루프를 사용하여 inventory 테이블의 각 항목을 확인하세요. 각 포션에 대해 pricestock을 곱하여 해당 포션의 가치를 구합니다. 그 숫자를 totalValue에 더하세요.

3. 루프가 끝난 후, 다음과 같이 결과를 출력하세요:

Total inventory value: 291 gold

문자열 연결 연산자(..)를 사용하여 텍스트와 totalValue의 값을 결합하세요.

직접 해보기

-- Create the initial inventory with health potion
inventory = {
    {name = "Health Potion", price = 15, stock = 8}
}

-- Create mana potion
manaPotion = {name = "Mana Potion", price = 12, stock = 5}

-- Add mana potion to inventory
table.insert(inventory, manaPotion)

-- Display all potions in inventory
for i = 1, #inventory do
    print(inventory[i].name .. ": " .. inventory[i].price .. " gold")
end

Fundamentals의 모든 레슨