39.1 Backpack Weight Calculator 1, practice (JS lessons)
๐ Task: Backpack Weight Calculator
Create an application "Backpack Weight Calculator".
Imagine you are going on a hike.
Before you leave, you need to pack your backpack.
The user must choose one item from each category.
After clicking the button, calculate the total weight of the backpack.
๐ฅค Category 1 โ Drink
- ๐ง Water โ 2 kg
- ๐ง Juice โ 3 kg
- ๐ฅค Soda โ 4 kg
- ๐ต Tea Thermos โ 5 kg
- ๐ฐ Water Canister โ 8 kg
๐ Category 2 โ Food
- ๐ Apples โ 2 kg
- ๐ฅช Sandwiches โ 3 kg
- ๐ฅซ Canned Food โ 5 kg
- ๐ฑ Food Pack โ 7 kg
- ๐ฆ Food Box โ 10 kg
๐ ๏ธ Category 3 โ Tool
- ๐ฆ Flashlight โ 1 kg
- ๐ช Knife โ 2 kg
- ๐ช Shovel โ 5 kg
- ๐ช Axe โ 7 kg
- ๐งฐ Tool Kit โ 10 kg
๐งฅ Category 4 โ Clothing
- ๐ T-Shirt โ 1 kg
- ๐งฅ Sweater โ 3 kg
- ๐งฅ Jacket โ 5 kg
- ๐ฅพ Warm Clothes โ 7 kg
- โ๏ธ Winter Clothes โ 10 kg
๐ Rules:
- Determine the weight of each selected item.
- Add up the weight of all four selected items.
- If the total weight is less than 10 kg โ display the message:
Too light!
- If the total weight is greater than 31 kg โ display the message:
Too heavy!
- If the weight is within the range of 7 to 31 kg โ display:
"Backpack weight: 24 kg"
๐ How it works:
- Get the values of all four
<select> elements.
- Use
switch to determine the weight of each selected item.
- Add all the values into a single variable.
- Check the total weight using
if.
- Display the final message.
๐งช Example:
The user selected:
- ๐ต Tea Thermos โ 5 kg
- ๐ฅซ Canned Food โ 5 kg
- ๐ช Axe โ 7 kg
- ๐งฅ Jacket โ 5 kg
Backpack weight: 25 kg