Complete solution (100% reference)
index.html- Main HTML pagescript.js- JavaScript logicstyles.css- Styling and layoutpackage.json- Dependenciessetup.sh- Setup scriptREADME.md- Instructions (below)💡 Download the ZIP, extract it, and follow the instructions below to get started!
This is the complete reference solution for Activity 08: Objects. This solution demonstrates best practices, comprehensive teaching comments, and serves as the gold standard for grading student submissions.
🎯 Learning Objectives:
⏱️ Estimated Time: 45-60 minutes
activity-08-object/
├── script.js # Complete solution with teaching comments
├── index.html # HTML structure with Exercise 3 button
├── package.json # Project dependencies (servor for dev server)
├── setup.sh # Setup script for environment
└── README.md # This instructor guide
⏱️ Time: 10-15 minutes
What Students Learn:
{ }Implementation:
var myPet = {
animal: "pug",
name: "Maya"
};
console.log(myPet.animal); // Output: pug
console.log(myPet.name); // Output: Maya
Key Teaching Points:
{ }, arrays use [ ]Common Student Mistakes:
[] instead of {} for objectsmyPet[name] instead of myPet.name or myPet["name"]Grading Checklist:
var myPet = { }animal property with a valuename property with a value⏱️ Time: 15-20 minutes
What Students Learn:
Implementation:
// Provided object
var movie = {
name: "Luca",
release: "June 18, 2021",
genre: "Comedy/Adventure",
duration: "1h 35m",
director: "Enrico Casarosa"
};
// Modify ALL properties (example with "Raya and the Last Dragon")
movie.name = "Raya and the Last Dragon";
movie.release = "March 5, 2021";
movie.genre = "Fantasy/Adventure";
movie.duration = "1h 47m";
movie.director = "Don Hall and Carlos López Estrada";
console.log(movie);
Key Teaching Points:
movie.name = "New" NOT var movie = { name: "New" }Common Student Mistakes:
== (comparison) instead of = (assignment){name: "Luca" = "New"} ❌movie.nam = "New" creates NEW property!Discussion Points:
movie.rating = "PG")delete movie.director)Grading Checklist:
movie object present in code⏱️ Time: 20-25 minutes
What Students Learn:
Implementation:
JavaScript (script.js):
// Select the button element
var lightBtn = document.querySelector('button');
// Create light object with method
var light = {
on: false, // Initial state: OFF
toggle: () => {
// Toggle the boolean state
light.on = !light.on;
// Update button based on state
if (light.on) {
lightBtn.textContent = "On";
lightBtn.classList.replace("btn-dark", "btn-warning");
} else {
lightBtn.textContent = "Off";
lightBtn.classList.replace("btn-warning", "btn-dark");
}
}
};
HTML (index.html):
<button class="btn btn-dark" onclick="light.toggle()">Off</button>
Key Teaching Points:
Object Methods: Functions stored as object properties
methodName: () => { code }objectName.methodName()Arrow Functions in Objects:
light.on = !light.onthis: this.on = !this.on (doesn't work with arrow functions!)this works, but we use arrow for this exerciseBoolean Toggle Pattern:
light.on = !light.on flips true <-> false!false → true!true → falseclassList.replace() vs add():
replace("old", "new") - swaps one class for anotheradd("new") - adds class but doesn't remove old oneBootstrap Classes:
btn-dark = dark grey (OFF state)btn-warning = yellow (ON state, like a light bulb!)Common Student Mistakes:
Arrow Function Issues:
toggle: function() { } - Using function keyword instead of arrowthis.on - Using this with arrow functions (doesn't work!)toggle: () => { } and light.on - Correct!Toggle Logic Errors:
light.on = true; - Hardcoded, doesn't togglelight.on = !true; - Always sets to falselight.on = !light.on; - Correctly togglesclassList Mistakes:
classList.add("btn-warning") - Adds without removing old classclassList.remove("btn-dark") then classList.add("btn-warning") - Works but verboseclassList.replace("btn-dark", "btn-warning") - Clean, atomicHTML Integration Errors:
onclick="toggle()" - Missing object referenceonclick="light.toggle" - Missing parenthesesonclick="light.toggle()" - Correct!Selector Issues:
document.querySelector('.button') - Wrong selectordocument.querySelector('button') - Correct!Incomplete Updates:
textContent, not classListclassList, not textContentTesting Checklist:
Before grading, verify:
Grading Checklist:
var lightBtn = document.querySelector('button') presenton: false propertylight.on = !light.ontextContent updates to "On"/"Off"classList.replace() swaps classes correctlyonclick="light.toggle()"Exercise One: Basic Object Creation (30 points)
Exercise 2: Property Modification (30 points)
Exercise 3: Object Methods (40 points)
on property (10 pts)!light.on) (10 pts)Likely causes:
How to fix:
// Check if object exists
console.log(myPet); // Should show full object
// Check property name
console.log(myPet.animal); // Not myPet.Animal or myPet.anmial
Likely causes:
onclick attribute in HTMLHow to fix:
<!-- WRONG -->
<button onclick="toggle()">Off</button>
<!-- CORRECT -->
<button onclick="light.toggle()">Off</button>
Check console (F12) for errors!
Likely causes:
classList.add() instead of classList.replace()How to fix:
// WRONG - adds class without removing old one
lightBtn.classList.add("btn-warning");
// CORRECT - replaces old class with new one
lightBtn.classList.replace("btn-dark", "btn-warning");
Check HTML <head> has Bootstrap CSS:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
Likely causes:
How to fix:
// WRONG - hardcoded
light.on = true;
// CORRECT - toggle with NOT operator
light.on = !light.on;
Add debugging:
console.log("Before:", light.on);
light.on = !light.on;
console.log("After:", light.on);
Add a property to track total clicks:
var light = {
on: false,
clickCount: 0,
toggle: () => {
light.clickCount++;
light.on = !light.on;
console.log("Total clicks:", light.clickCount);
// ... rest of code
}
};
var light = {
state: 0, // 0=off, 1=dim, 2=medium, 3=bright
toggle: () => {
light.state = (light.state + 1) % 4; // Cycle 0→1→2→3→0
// Update button color based on state
}
};
Three buttons that cycle through states with appropriate colors.
Light automatically turns off after 5 seconds:
toggle: () => {
light.on = !light.on;
if (light.on) {
setTimeout(() => {
light.on = false;
// Update button to OFF
}, 5000); // 5 seconds
}
}
Create 3 separate light objects with 3 buttons that toggle independently.
Use this when grading student submissions:
{ } not [ ]animal and namemovie object code is presentquerySelectoron: false property() => { }!light.on patterntextContent AND classListclassList.replace() not just add()onclick="light.toggle()"this keyword issue with arrow functionslight.on = !light.on toggle instead of breaking?"Misconception: Students confuse { } and [ ]
Clarification:
{ } -> Key-value pairs, unordered[ ] -> Indexed list, orderedMisconception: this.on should work in arrow functions
Clarification:
thislight.onthis to workMisconception: Adding new class without removing old one
Clarification:
add() -> Can have multiple classes (btn-dark AND btn-warning = undefined behavior)replace() -> Swaps classes atomically, ensures only one activeMisconception: Can't add properties later
Clarification:
myPet.age = 3; (creates new property)delete myPet.age;Navigate to solution directory:
cd "Paid Courses/W2 Advanced JavaScript Foundations/Template Solution/activity-08-object"
Run setup script:
chmod +x setup.sh
./setup.sh
Start development server:
npm start
Open in browser:
http://localhost:8080Test all exercises:
Simply open index.html directly in a browser. Console logging and button will work, but no live reload.
this keyword deep diveactivity-08-object/
│
├── script.js ← Complete solution with teaching comments
│ ├── Exercise 1: Pet object (myPet)
│ ├── Exercise 2: Movie object modification
│ ├── Exercise 3: Light object with toggle method
│ └── Comprehensive instructor notes
│
├── index.html ← HTML structure
│ ├── Bootstrap 4.6.2 CSS
│ ├── Exercise 3 button with onclick
│ └── Script tag linking to script.js
│
├── package.json ← Dependencies (servor for dev server)
├── setup.sh ← Automated setup script
└── README.md ← This comprehensive instructor guide
For questions about this solution:
script.jsFor curriculum questions:
Last Updated: 2025-10-10 Solution Version: One.0 Based on Pattern: Project 01 BMI Calculator Template Solution