🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Project 19: Profile Settings Card

JS Wizard

Builds on these lessons

Step 1 of 2
Project

The Observer Pattern

Build a ThemeSubject object with a list of subscriber functions, a subscribe method to add one, and a notify method that calls every subscriber with the new theme. Each subscriber reacts to a change without ThemeSubject needing to know anything about what they do.

🎯 Your Task

Please add the exact code shown in the light gray box below to your editor.Do not delete your previous code, just insert these new lines in the correct place!

const ThemeSubject = {
  subscribers: [],
  subscribe(fn) {
    this.subscribers.push(fn);
  },
  notify(theme) {
    this.subscribers.forEach((fn) => fn(theme));
  }
};

ThemeSubject.subscribe((theme) => console.log("Sidebar updated to " + theme));
ThemeSubject.subscribe((theme) => console.log("Header updated to " + theme));

ThemeSubject.notify("dark");