Practice and reinforce the concepts from Lesson 12
In this debugging challenge, you will:
⚠️ Important: Before You Start DO NOT DELETE any existing files in the template:
- Package files
- Configuration files
- Any files you didn't create
ONLY EDIT the necessary JavaScript and HTML files for each challenge.
drinks
with 5 drink elementsconsole.log()
Expected output in browser console (F12):
Assuming the drinks are Coffee, Tea, Milo, Horlicks, and Apple Juice.
pop()
to remove the last element from drinks arraypush("Matcha")
to add a new drink to the arrayExpected output:
💡 Helpful Hint Remember that
pop()
removes AND returns the last element, whilepush()
adds elements to the end of the array.
Use the forEach()
method to log each drink element individually to the console.
Expected output:
💡 Reference Check the cheatsheet for
forEach()
method examples if you need help with the syntax!
Create the change function in script.js:
change
drinkIndex
variable using prompt("Which drink do you want to replace?")
newDrink
variable using prompt("What is the new drink?")
drinks[drinkIndex] = newDrink
Add the button to index.html:
<button class="btn btn-primary" onclick="change()">CHANGE</button>
Expected result:
When clicking the button:
Example: Replacing drink at index 3 with Mineral Water
Console output after change:
const ul = document.querySelector("ul")
const newli = document.createElement("li")
newli.textContent = "Giraffe"
ul.appendChild(newli)
Expected result:
const h2 = document.querySelector("#heading2")
h2.remove()
Expected result:
💡 Common Mistake Make sure the element exists before trying to remove it! Check that your selector is correct.
const style = document.querySelector("#style")
style.classList.add("bg-warning")
style.classList.add("m-5")
Expected result:
profile
with these 4 properties:
name
(string)gender
(string)age
(number)school
(string)Example code structure:
const profile = {
name: "Maya",
gender: "Female",
age: 14,
school: "Telebort"
};
console.log(profile);
Expected console output:
school
property to an array containing multiple schoolsExample:
profile.school = ["ABC Primary School", "XYZ Secondary School", "Telebort"];
console.log(profile);
Expected console output:
💡 Helpful Hint You can modify object properties after creation using dot notation (object.property) or bracket notation (object['property']).
Add this button element to your index.html file:
<button class="btn btn-light">Click me</button>
Select the button element:
const button = document.querySelector('button');
Add mouseover event listener:
button.addEventListener('mouseover', () => {
button.classList.replace('btn-light', 'btn-warning');
});
Add mouseout event listener:
button.addEventListener('mouseout', () => {
button.classList.replace('btn-warning', 'btn-light');
});
Expected result:
💡 Key Concepts
addEventListener()
attaches event handlers to elements- Arrow functions
() => {}
provide a concise way to write functionsclassList.replace()
swaps one class for another
Add a click event that shows an alert:
button.addEventListener('click', () => {
alert('I love Telebort!');
});
Expected result when clicking:
💡 Debugging Help If your events aren't working:
- Check that your button selector is correct
- Ensure your script is loaded after the HTML elements
- Open the browser console (F12) to check for errors
Before submitting, ensure your project includes:
push()
and pop()
methods working correctlyforEach()
method logging each elementcreateElement()
and appendChild()
remove()
methodclassList
⚠️ Important: Test Before Submitting! Before submission, test ALL functionality:
- Open browser console (F12) to check for errors
- Test all buttons and interactions
- Verify console outputs match expected results
- Ensure all 4 challenges are complete
Ready to submit? Use the form below:
onclick
attributeconsole.log(drinks.length)
to see array size💡 Need Help? If you're stuck on any challenge, try:
- Reading the error messages in the console
- Adding
console.log()
statements to debug- Reviewing the lesson materials
- Asking for help in the discussion forum