39.2 Backpack Weight Calculator 2, practice (JS lessons)
๐ Task: Backpack Weight Calculator 2
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,
then choose the hike difficulty.
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
๐๏ธ Category 5 โ Hike difficulty
- ๐ Easy โ max 20 kg
- ๐ Normal โ max 32 kg
- ๐ Hard โ max 40 kg
๐ Rules:
- Determine the weight of each selected item.
- Display the weight of each selected item.
- Add up the weight of all four selected items.
- The maximum allowed weight depends on the selected difficulty.
๐ Messages:
- If the total weight is less than 10 kg โ display:
โ Too light!
- If the total weight is within the range of 11โ20 kg โ display:
๐ Good weight!
- If the total weight is within the range of 21 kg up to the maximum weight of the selected
difficulty โ display:
โ
Perfect backpack!
- If the total weight is greater than the maximum weight of the selected difficulty โ
display:
โ Too heavy!
๐ How it works:
- Get the values of all five
<select> elements.
- Use
switch to determine the weight of each selected item.
- Determine the maximum allowed weight based on the hike difficulty.
- Calculate the total weight.
- Check the result using
if.
- Display the full report.
๐งช Example:
The user selected:
- ๐ต Tea Thermos โ 5 kg
- ๐ฅซ Canned Food โ 5 kg
- ๐ช Axe โ 7 kg
- ๐ฅผ Jacket โ 5 kg
- ๐ Normal
Drink: 5 kg
Food: 5 kg
Tool: 7 kg
Clothes: 5 kg
----------------
Total weight: 22 kg
โ
Perfect backpack!
๐ Important:
- Use
switch to determine the weight of each item.
- Create a separate variable for each item's weight.
- Use
if to determine the message.
- The maximum weight depends on the selected hike difficulty.
- Display the full report with the weight of each selected item and the total weight.