Problem: Create a Feature-Flagged Analytics Service
Problem statement
You’re rolling out a new analytics module, but it’s not yet available to all users. It’s gated behind a flags.analyticsEnabled feature flag. The team needs a factory that returns either:
A real
AnalyticsServicewhen the flag is on.A stub that does nothing when the flag is off.
Both methods must be exposed: .track() and .report(). This ensures the rest of the codebase can safely call the service without knowing which version was returned.
Goal
Implement createAnalyticsService(flags) that returns either a real or stub service, with matching method signatures.
Constraints
Do not use conditionals (
if,switch, ternaries) to select which class to instantiate.The factory must return an object with
.track()and.report()either way.Stub and real classes must only be instantiated within the factory.
Sample output
The examples below illustrate what the output should look like:
const flags1 = { analyticsEnabled: true };const analytics1 = createAnalyticsService(flags1);analytics1.track('user.signup');analytics1.report();/* Expected output:AnalyticsService initializedTracking event: user.signupGenerating report */const flags2 = { analyticsEnabled: false };const analytics2 = createAnalyticsService(flags2);analytics2.track('user.login');analytics2.report();/* No output expected */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Create a Feature-Flagged Analytics Service
Problem statement
You’re rolling out a new analytics module, but it’s not yet available to all users. It’s gated behind a flags.analyticsEnabled feature flag. The team needs a factory that returns either:
A real
AnalyticsServicewhen the flag is on.A stub that does nothing when the flag is off.
Both methods must be exposed: .track() and .report(). This ensures the rest of the codebase can safely call the service without knowing which version was returned.
Goal
Implement createAnalyticsService(flags) that returns either a real or stub service, with matching method signatures.
Constraints
Do not use conditionals (
if,switch, ternaries) to select which class to instantiate.The factory must return an object with
.track()and.report()either way.Stub and real classes must only be instantiated within the factory.
Sample output
The examples below illustrate what the output should look like:
const flags1 = { analyticsEnabled: true };const analytics1 = createAnalyticsService(flags1);analytics1.track('user.signup');analytics1.report();/* Expected output:AnalyticsService initializedTracking event: user.signupGenerating report */const flags2 = { analyticsEnabled: false };const analytics2 = createAnalyticsService(flags2);analytics2.track('user.login');analytics2.report();/* No output expected */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.