Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7242f4

Browse files
committed
feat: Implement 2 functions in array exercise 17
- Add fn. 1: `displayInventory()` - Add fn. 2: `addItem()` - Update the README with more comprehensive and clear test scenario
1 parent 9396b18 commit b7242f4

File tree

2 files changed

+132
-51
lines changed

2 files changed

+132
-51
lines changed

‎README.md‎

Lines changed: 102 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ These exercises were developed as part of:
8585
![Status](https://img.shields.io/badge/Status-Complete-brightgreen)
8686

8787
* Exercises **10 - 11**
88-
* Started: June 2025
89-
* Completed: June 2025
88+
* Started: July 2025
89+
* Completed: July 2025
9090

9191
**III. Additional Practice Exercises**
9292

@@ -244,7 +244,7 @@ Write a program which prints out the lyrics to that beloved classic, that field-
244244

245245
- You can't stop talking to grandma until you shout "*BYE*".
246246

247-
💡 **Hint**: Don't forget 'BYE' is not the same as 'BYE ' or ' BYE '!
247+
💡 **Hint**: Don't forget 'BYE' is not the same as 'BYE ' or ' BYE '.
248248
Use `trim()`.
249249
</details>
250250

@@ -269,7 +269,7 @@ Write a program which prints out the lyrics to that beloved classic, that field-
269269
**Saying Bye to Grandma:**
270270
You can't stop talking to grandma until you shout 'BYE' 3 times **in a row** (If you say 'BYE' and then anything else, it won't count).
271271

272-
💡 **Hint**: Don't forget 'BYE' is not the same as 'BYE ' or ' BYE '! Use `trim()`.
272+
💡 **Hint**: Don't forget 'BYE' is not the same as 'BYE ' or ' BYE '. Use `trim()`.
273273

274274
**Feature:** More phrases stored in arrays that Grandma speaks randomly (depending on the input conditions).
275275
</details>
@@ -671,6 +671,7 @@ Expected Output (for adding 'Healing Potion'):
671671

672672
```javascript
673673
Adding Healing Potion...
674+
Item added: Healing Potion
674675
Your inventory: Sword, Shield, Potion, Bow, Arrows, Map, Healing Potion
675676
```
676677

@@ -810,55 +811,105 @@ After implementing all the functions, add the following code to your .js file (b
810811
This sequence of calls will demonstrate if your inventory manager works correctly.
811812

812813
```javascript
813-
console.log("--- Initial Inventory ---");
814-
displayInventory();
815-
816-
console.log("\n--- Scenario 1: Adding Items ---");
817-
addItem('Healing Potion'); // Add a new item
818-
addItem('Map'); // Try to add a duplicate item
819-
addItem('Gold Coins'); // Add another new item
820-
821-
console.log("\n--- Scenario 2: Removing Items ---");
822-
removeItem('Shield'); // Remove an existing item
823-
removeItem('NonExistentItem'); // Try to remove a non-existent item
824-
removeItem('Arrows'); // Remove another existing item
825-
826-
console.log("\n--- Scenario 3: Checking for Items ---");
827-
hasItem('Sword'); // Check for an existing item
828-
hasItem('Gold Coins'); // Check for a newly added item
829-
hasItem('Magic Orb'); // Check for a non-existent item
830-
831-
console.log("\n--- Scenario 4: Sorting Inventory ---");
832-
sortInventory();
833-
834-
console.log("\n--- Scenario 5: Item Transformations (map) ---");
835-
const lengths = getItemLengths(); // Call the function, it should log its output internally
836-
console.log(`Item lengths array returned: ${lengths}`);
837-
838-
console.log("\n--- Scenario 6: Item Filtering (filter) ---");
839-
const shortItems = getShortItems(5); // Call the function, it should log its output internally
840-
console.log(`Short items array returned: ${shortItems}`);
841-
const veryShortItems = getShortItems(3);
842-
console.log(`Very short items array returned: ${veryShortItems}`);
843-
844-
845-
console.log("\n--- Scenario 7: Calculating Total Value (reduce) ---");
846-
const itemPriceList = {
847-
'Sword': 100,
848-
'Shield': 80, // Note: Shield might be removed earlier depending on exact test order
849-
'Potion': 20,
850-
'Bow': 70,
851-
'Arrows': 5,
852-
'Map': 30,
853-
'Healing Potion': 25,
854-
'Gold Coins': 10
855-
// NonExistentItem and Magic Orb have 0 value as they are not here
814+
let myAdventureInventory = ['Steel Sword', 'Leather Armor', 'Health Potion', 'Magical Map'];
815+
816+
console.log("--- My Adventure Inventory Test ---");
817+
818+
console.log("My Adventure Inventory: Initial State");
819+
displayInventory(myAdventureInventory);
820+
821+
console.log("Adding New Gear...");
822+
addItem(myAdventureInventory, 'Elixir of Life');
823+
addItem(myAdventureInventory, 'Magical Map'); // This should fail gracefully
824+
addItem(myAdventureInventory, 'Rope');
825+
826+
console.log("Removing Unwanted Items...");
827+
removeItem(myAdventureInventory, 'Leather Armor');
828+
removeItem(myAdventureInventory, 'Rusty Dagger'); // This should fail as it's not present
829+
removeItem(myAdventureInventory, 'Health Potion');
830+
831+
console.log("Is the Gear Present?");
832+
hasItem(myAdventureInventory, 'Steel Sword');
833+
hasItem(myAdventureInventory, 'Elixir of Life');
834+
hasItem(myAdventureInventory, 'Health Potion'); // This should fail as it was removed
835+
836+
console.log("Sorting My Inventory...");
837+
sortInventory(myAdventureInventory);
838+
839+
console.log("Analyzing Item Data (Map/Filter)...");
840+
const myLengths = getItemLengths(myAdventureInventory);
841+
console.log(`Item lengths array returned: ${myLengths}`);
842+
const myShortItems = getShortItems(myAdventureInventory, 6);
843+
console.log(`Short items array returned: ${myShortItems}`);
844+
845+
console.log("Calculating Total Value...");
846+
const myPriceList = {
847+
'Steel Sword': 150,
848+
'Leather Armor': 75,
849+
'Health Potion': 20,
850+
'Magical Map': 50,
851+
'Elixir of Life': 200,
852+
'Rope': 5
856853
};
857-
const totalValue = calculateTotalValue(itemPriceList); // Call the function, it should log its output internally
858-
console.log(`Total inventory value returned: ${totalValue} gold`);
854+
const myTotalValue = calculateTotalValue(myAdventureInventory, myPriceList);
855+
console.log(`Total inventory value: ${myTotalValue} gold`);
856+
857+
console.log("My Adventure Inventory: Final State");
858+
displayInventory(myAdventureInventory);
859+
```
860+
861+
862+
Expected Output:
863+
864+
```javascript
865+
--- My Adventure Inventory Test ---
866+
My Adventure Inventory: Initial State
867+
Your inventory: Steel Sword,Leather Armor,Health Potion,Magical Map
868+
869+
Adding New Gear...
870+
Adding Elixir of Life...
871+
Item added: Elixir of Life
872+
Your inventory: Steel Sword,Leather Armor,Health Potion,Magical Map,Elixir of Life
873+
Adding Magical Map...
874+
You already have Magical Map in your inventory.
875+
Your inventory: Steel Sword,Leather Armor,Health Potion,Magical Map,Elixir of Life
876+
Adding Rope...
877+
Item added: Rope
878+
Your inventory: Steel Sword,Leather Armor,Health Potion,Magical Map,Elixir of Life,Rope
879+
880+
Removing Unwanted Items...
881+
Attempting to remove Leather Armor...
882+
Leather Armor removed from inventory.
883+
Your inventory: Steel Sword,Health Potion,Magical Map,Elixir of Life,Rope
884+
Attempting to remove Rusty Dagger...
885+
Item Rusty Dagger not found in inventory.
886+
Your inventory: Steel Sword,Health Potion,Magical Map,Elixir of Life,Rope
887+
Attempting to remove Health Potion...
888+
Health Potion removed from inventory.
889+
Your inventory: Steel Sword,Magical Map,Elixir of Life,Rope
890+
891+
Is the Gear Present?
892+
You have Steel Sword in your inventory.
893+
You have Elixir of Life in your inventory.
894+
You do not have Health Potion in your inventory.
895+
896+
Sorting My Inventory...
897+
Sorting inventory...
898+
Your inventory: Elixir of Life,Magical Map,Rope,Steel Sword
899+
900+
Analyzing Item Data (Map/Filter)...
901+
Item name lengths: 14,12,4,11
902+
Item lengths array returned: 14,12,4,11
903+
Short items (<= 6 chars): Rope
904+
Short items array returned: Rope
905+
906+
Calculating Total Value...
907+
Calculating total inventory value...
908+
Total inventory value: 355 gold
909+
Total inventory value returned: 355 gold
859910

860-
console.log("\n--- Final Inventory State ---");
861-
displayInventory(); // Display inventory one last time after all operations
911+
My Adventure Inventory: Final State
912+
Your inventory: Elixir of Life,Magical Map,Rope,Steel Sword
862913
```
863914
</details>
864915

‎array-exercises/17_gameInventory.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
let inventory = ['Sword', 'Shield', 'Potion', 'Bow', 'Arrows', 'Map'];
3+
4+
// 1 - Display Inventory
5+
6+
console.groupCollapsed(`Display Inventory`);
7+
function displayInventory(inventory){
8+
console.log(`📜 Your inventory: ${inventory}`);
9+
return inventory;
10+
}
11+
displayInventory(inventory);
12+
console.groupEnd();
13+
14+
// 2 - Add Item
15+
16+
console.groupCollapsed(`Add item`);
17+
function addItem(itemName){
18+
console.log (`Adding ${itemName}... ⏳`);
19+
if (inventory.includes(itemName)){
20+
console.log(`You already have ${itemName} in your inventory.`);
21+
}else{
22+
inventory.push(itemName);
23+
console.log(`Item added: ${itemName} ✨ `);
24+
}
25+
console.log(`📜 Your inventory: ${inventory}`)
26+
return inventory;
27+
}
28+
addItem (`Healing Potion`);
29+
addItem (`Map`);
30+
console.groupEnd();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /