Problem: Build a Minimal Email Template Builder

Easy
15 min
Implement a fluent builder to construct basic email templates.

Problem statement

Your team needs a flexible way to construct transactional emails, such as welcome messages, password resets, and usage alerts. Right now, it’s all handled with fragile object literals and scattered logic. You’re refactoring toward a more expressive API that reads like a recipe.

Goal

Create an EmailBuilder class with the following chainable methods:

  • .to(email: string)

  • .withSubject(subject: string)

  • .withBody(content: string)

  • .build(): Returns the final object with shape :{ to, subject, body }

All fields are required before calling .build(). If any field is missing, throw an error.

Constraints

  • Do not use constructor parameters to initialize the email.

  • Do not allow optional chaining or fallback defaults inside .build().

  • Use method chaining to construct the object—no helper function wrappers.

Sample output

The examples below illustrate what the output should look like:

// Happy path
const email1 = new EmailBuilder()
.to('user1@example.com')
.withSubject('Welcome!')
.withBody('Glad you’re here.')
.build();
console.log(email1);
/* Expected output:
{ to: 'user1@example.com',
subject: 'Welcome!',
body: 'Glad you’re here.' } */
// Missing subject
try {
const email2 = new EmailBuilder()
.to('user2@example.com')
.withBody('No subject line.')
.build();
} catch (err) {
console.log('Failed to build email2:', err.message);
}
/* Expected output:
Failed to build email2: Missing required email fields */
// Another valid email (out-of-order chaining)
const email3 = new EmailBuilder()
.withBody('Don’t forget our meeting.')
.withSubject('Reminder')
.to('user3@example.com')
.build();
console.log(email3);
/* Expected output:
{ to: 'user3@example.com',
subject: 'Reminder',
body: 'Don’t forget our meeting.' } */
// Missing recipient
try {
const email4 = new EmailBuilder()
.withSubject('No recipient')
.withBody('We need someone to send this to.')
.build();
} catch (err) {
console.log('Failed to build email4:', err.message);
}
/* Expected output:
Failed to build email4: Missing required email fields */

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

Problem: Build a Minimal Email Template Builder

Easy
15 min
Implement a fluent builder to construct basic email templates.

Problem statement

Your team needs a flexible way to construct transactional emails, such as welcome messages, password resets, and usage alerts. Right now, it’s all handled with fragile object literals and scattered logic. You’re refactoring toward a more expressive API that reads like a recipe.

Goal

Create an EmailBuilder class with the following chainable methods:

  • .to(email: string)

  • .withSubject(subject: string)

  • .withBody(content: string)

  • .build(): Returns the final object with shape :{ to, subject, body }

All fields are required before calling .build(). If any field is missing, throw an error.

Constraints

  • Do not use constructor parameters to initialize the email.

  • Do not allow optional chaining or fallback defaults inside .build().

  • Use method chaining to construct the object—no helper function wrappers.

Sample output

The examples below illustrate what the output should look like:

// Happy path
const email1 = new EmailBuilder()
.to('user1@example.com')
.withSubject('Welcome!')
.withBody('Glad you’re here.')
.build();
console.log(email1);
/* Expected output:
{ to: 'user1@example.com',
subject: 'Welcome!',
body: 'Glad you’re here.' } */
// Missing subject
try {
const email2 = new EmailBuilder()
.to('user2@example.com')
.withBody('No subject line.')
.build();
} catch (err) {
console.log('Failed to build email2:', err.message);
}
/* Expected output:
Failed to build email2: Missing required email fields */
// Another valid email (out-of-order chaining)
const email3 = new EmailBuilder()
.withBody('Don’t forget our meeting.')
.withSubject('Reminder')
.to('user3@example.com')
.build();
console.log(email3);
/* Expected output:
{ to: 'user3@example.com',
subject: 'Reminder',
body: 'Don’t forget our meeting.' } */
// Missing recipient
try {
const email4 = new EmailBuilder()
.withSubject('No recipient')
.withBody('We need someone to send this to.')
.build();
} catch (err) {
console.log('Failed to build email4:', err.message);
}
/* Expected output:
Failed to build email4: Missing required email fields */

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

Node.js
class EmailBuilder {
// implement methods here
}
// Example usage:
// Happy path
const email1 = new EmailBuilder()
.to('user1@example.com')
.withSubject('Welcome!')
.withBody('Glad you’re here.')
.build();
console.log(email1);
/* Expected output:
{ to: 'user1@example.com',
subject: 'Welcome!',
body: 'Glad you’re here.' } */
// Missing subject
try {
const email2 = new EmailBuilder()
.to('user2@example.com')
.withBody('No subject line.')
.build();
} catch (err) {
console.log('Failed to build email2:', err.message);
}
/* Expected output:
Failed to build email2: Missing required email fields */
// Another valid email (out-of-order chaining)
const email3 = new EmailBuilder()
.withBody('Don’t forget our meeting.')
.withSubject('Reminder')
.to('user3@example.com')
.build();
console.log(email3);
/* Expected output:
{ to: 'user3@example.com',
subject: 'Reminder',
body: 'Don’t forget our meeting.' } */
// Missing recipient
try {
const email4 = new EmailBuilder()
.withSubject('No recipient')
.withBody('We need someone to send this to.')
.build();
} catch (err) {
console.log('Failed to build email4:', err.message);
}
/* Expected output:
Failed to build email4: Missing required email fields */