Problem: Format Greetings in Multiple Styles

Easy
15 min
Build a message formatter that can switch between multiple output styles—casual, formal, and JSON—without modifying the main formatting logic.

Problem statement

Your team is building a notification system that sends personalized greetings. Different teams want the message formatted differently:

  • Marketing prefers a casual tone.

  • HR needs a formal version.

  • The API team wants the data in JSON format.

Message formatters often rely on conditionals to handle different styles, which makes them brittle as formats evolve. Every new style risks breaking existing logic. Build a flexible greeting service using the Strategy Pattern, allowing the output style to be swapped cleanly at runtime.

Goal

Implement both elements of the pattern:

  1. Strategies:

    1. Create CasualGreetingStrategyFormalGreetingStrategy, and JsonGreetingStrategy classes.

    2. Each must define a .format(user) method that returns a message string or object in its respective style.

  2. Context:

    1. Implement GreetingService to delegate message formatting to whichever strategy is active.

    2. Allow switching strategies with the .setStrategy() method.

Constraints

  • No if or switch statements in GreetingService.

  • Each strategy must be self-contained—no shared formatting helpers.

  • Adding a new format should not require modifying existing strategies or the service.

Sample output

The examples below illustrate what the output should look like:

const user = { name: 'Alice', title: 'Engineer' };
const service = new GreetingService(new CasualGreetingStrategy());
console.log('Casual:', service.greet(user));
/* Expected output:
Casual: Hey Alice! Welcome back.
*/
service.setStrategy(new FormalGreetingStrategy());
console.log('Formal:', service.greet(user));
/* Expected output:
Formal: Good afternoon, Engineer Alice.
*/
service.setStrategy(new JsonGreetingStrategy());
console.log('JSON:', service.greet(user));
/* Expected output:
JSON: {"greeting":"Hello","name":"Alice","title":"Engineer"}
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Format Greetings in Multiple Styles

Easy
15 min
Build a message formatter that can switch between multiple output styles—casual, formal, and JSON—without modifying the main formatting logic.

Problem statement

Your team is building a notification system that sends personalized greetings. Different teams want the message formatted differently:

  • Marketing prefers a casual tone.

  • HR needs a formal version.

  • The API team wants the data in JSON format.

Message formatters often rely on conditionals to handle different styles, which makes them brittle as formats evolve. Every new style risks breaking existing logic. Build a flexible greeting service using the Strategy Pattern, allowing the output style to be swapped cleanly at runtime.

Goal

Implement both elements of the pattern:

  1. Strategies:

    1. Create CasualGreetingStrategyFormalGreetingStrategy, and JsonGreetingStrategy classes.

    2. Each must define a .format(user) method that returns a message string or object in its respective style.

  2. Context:

    1. Implement GreetingService to delegate message formatting to whichever strategy is active.

    2. Allow switching strategies with the .setStrategy() method.

Constraints

  • No if or switch statements in GreetingService.

  • Each strategy must be self-contained—no shared formatting helpers.

  • Adding a new format should not require modifying existing strategies or the service.

Sample output

The examples below illustrate what the output should look like:

const user = { name: 'Alice', title: 'Engineer' };
const service = new GreetingService(new CasualGreetingStrategy());
console.log('Casual:', service.greet(user));
/* Expected output:
Casual: Hey Alice! Welcome back.
*/
service.setStrategy(new FormalGreetingStrategy());
console.log('Formal:', service.greet(user));
/* Expected output:
Formal: Good afternoon, Engineer Alice.
*/
service.setStrategy(new JsonGreetingStrategy());
console.log('JSON:', service.greet(user));
/* Expected output:
JSON: {"greeting":"Hello","name":"Alice","title":"Engineer"}
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// --- Strategies (implement these) ---
class CasualGreetingStrategy {}
class FormalGreetingStrategy {}
class JsonGreetingStrategy {}
// --- Context (implement this too) ---
class GreetingService {
constructor(strategy) {}
setStrategy(strategy) {}
greet(user) {}
}
// Example usage
const user = { name: 'Alice', title: 'Engineer' };
const service = new GreetingService(new CasualGreetingStrategy());
console.log('Casual:', service.greet(user));
/* Expected output:
Casual: Hey Alice! Welcome back.
*/
service.setStrategy(new FormalGreetingStrategy());
console.log('Formal:', service.greet(user));
/* Expected output:
Formal: Good afternoon, Engineer Alice.
*/
service.setStrategy(new JsonGreetingStrategy());
console.log('JSON:', service.greet(user));
/* Expected output:
JSON: {"greeting":"Hello","name":"Alice","title":"Engineer"}
*/