๐งช Week 5 โ Guided Practice (a.k.a. List Wizard Training ๐งโโ๏ธ)
This week, you'll harness the ancient powers of JavaScript to add, remove, and replace items in an array โ and make the DOM dance with your commands. All through the console, like a true wizard.
๐ ๏ธ Setup
Create a file named index.html with this magical HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Week 5 โ List Wizard Training ๐งโโ๏ธ</title>
<style>
body { font-family: system-ui, Arial, sans-serif; line-height: 1.5; padding: 2rem; }
h1 { margin-top: 0; }
.card { border: 1px solid #ddd; border-radius: 12px; padding: 1rem 1.25rem; }
ul { margin: .5rem 0 0; padding-left: 1.25rem; }
code { background: #f6f8fa; padding: .1rem .35rem; border-radius: 6px; }
.hint { color: #555; font-size: 0.95rem; }
</style>
</head>
<body>
<header>
<h1>๐งช Week 5 โ Guided Practice (List Wizard Training)</h1>
<p class="hint">Open your browser console (<code>Ctrl/โ + Shift + J</code>) to cast your spells.</p>
</header>
<main>
<section class="card" aria-labelledby="inv-title">
<h2 id="inv-title">๐ Your Spellbook (Inventory)</h2>
<!-- This is the exact target your JS practice uses -->
<span id="items" aria-live="polite">
<!-- JS will render a <ul><li>โฆ</li></ul> here -->
</span>
<p class="hint">
Try in the console:
<code>addItem('๐งช Potion of Wisdom')</code>,
<code>removeItem(0)</code>,
<code>replaceItem(0, 'Phoenix Feather')</code>
</p>
</section>
<section style="margin-top:1.5rem;">
<h3>๐งโโ๏ธ Notes</h3>
<ul>
<li><code>addItem</code> uses <code>push()</code> to add to the array and then updates the DOM.</li>
<li><code>removeItem(index)</code> uses <code>splice()</code> to delete by position.</li>
<li><code>replaceItem(index, newItem)</code> swaps an existing item (via assignment or <code>splice()</code>).</li>
<li><code>displayItems()</code> rebuilds the list inside <code><span id="items"></code>.</li>
</ul>
</section>
</main>
<noscript>This practice requires JavaScript. Please enable it to continue your wizardry. ๐ช</noscript>
<!-- ๐งโโ๏ธ โThis is your enchanted scroll. The spells (scripts) go at the bottom!โ -->
<script src="script.js"></script>
</body>
</html>
๐งโโ๏ธ โThis is your enchanted scroll. The spells (scripts) go at the bottom!โ
Then summon your script.js like the coding sorcerer you are. Youโll write your magic there.
๐งโโ๏ธ Your Missions (a.k.a. Steps)
โจ Step 1: Create an Empty Array
Create an empty array called items.
You now have a list! It holdsโฆ nothing. But hey, it's full of potential.
๐ Step 2: Write a Function to Add Items
Define a function called addItem. It should take one parameter and use .push() to add the item to your magical bag of items.
Think of
.push()as throwing stuff into your backpack.
๐ Step 3: Show the List to the World
- Use the DOM to find the
<span id="items">in your HTML. - Clear it, then use a loop to add every item.
- Donโt forget to
.join()the items like a playlist of your favorite chaos.
Pro tip: Clear the old list first, or youโll end up stacking ghosts of past items.
๐งช Step 4: Test Your Function Like a Mad Scientist
In the console:
addItem('๐งช Potion of Wisdom');
addItem('๐ฎ Magic Taco');
addItem('๐ Baby Dragon');
๐งโโ๏ธ Your DOM should now resemble a spellbook full of weird magical loot. If it doesnโt, blame the Magic Taco. ๐ฎโจ
๐งฝ Step 5: Remove an Item (gently or violently)
Create a function removeItem(index) and use .splice() to delete the item at that index.
Itโs like popping a bubble in your arrayโvery satisfying.
๐ฃ Step 6: Add a Talking Feature
Create a displayMessage() function to show a message when something happens.
Because if your code doesn't talk to you, is it even alive?
๐ณ๏ธ Step 7: Try Deleting Things
removeItem(1); // Magic Taco vanishes into a swirling portal of mystery ๐ฎ๐
๐ง Step 8: Replace an Item
Define replaceItem(index, newItem) using splice() (or direct assignment) to swap an item.
replaceItem(0, '๐ฆ Phoenix Feather'); // Because nothing says upgrade like legendary loot. ๐ฅ
๐ฏ Final Challenge: Fancy It Up
Display your list as proper <ul><li></li></ul> HTML.
Letโs get civilizedโenough of this comma-separated chaos.
โ๏ธ Final Version (with Comments)
Yes, it's functional. Yes, it works. And yes, it might even impress your code crush ๐.
// ๐งโโ๏ธ An enchanted inventory system for magical items
// Start with an empty inventory array
const items = [];
// ๐งช Function to add an item to your wizard bag
function addItem(item) {
items.push(item); // Add the new item
displayItems(); // Refresh the list in the HTML
displayMessage(`๐ง Added: ${item}`); // Console log a fun message
}
// ๐ฅ Function to remove an item by its index
function removeItem(index) {
if (index >= 0 && index < items.length) {
const removed = items.splice(index, 1); // Remove the item
displayItems(); // Refresh the list
displayMessage(`๐ฅ ${removed[0]} vanished into the void! ๐`); // Dramatic exit message
} else {
displayMessage("โ ๏ธ That item index doesnโt exist in your spellbook!"); // Invalid index warning
}
}
// ๐งพ Function to replace one item with another
function replaceItem(index, newItem) {
if (index >= 0 && index < items.length) {
const oldItem = items[index]; // Store the old item
items[index] = newItem; // Replace with the new one
displayItems(); // Update the visual list
displayMessage(`Replaced "${oldItem}" with "${newItem}". Excellent swap!`);
} else {
displayMessage("๐ That index is beyond the bounds of your wizard bag.");
}
}
// ๐งพ Function to display all items in the list
function displayItems() {
const span = document.getElementById("items");
span.innerHTML = ""; // Clear the old list
const listItems = []; // Start with an empty array
for (const item of items) {
listItems.push(`<li>${item}</li>`);
}
// Wrap the joined items with <ul> tags
span.innerHTML = `<ul>${listItems.join("")}</ul>`;
}
// ๐ฃ Function to display a console message
function displayMessage(msg) {
console.log(msg); // Send message to console
}
// ๐ Summon initial magical items
addItem('๐งช Potion of Wisdom');
addItem('๐ Ancient Spell Scroll');
addItem('๐ Baby Dragon');
addItem('๐ฎ Magic Taco'); // Because obviously
// ๐งน Practice casting spells on the inventory
removeItem(1); // Remove the scroll
replaceItem(0, '๐งช Phoenix Feather'); // Upgrade the potion