Codington
0 of 6 problems solved • 0%

Exercise prompt

Greeting with Optional Title

Optional parameters let you model inputs that may or may not be provided. In TypeScript, an optional parameter x?: T is seen as T | undefined at the type level.

You’ll write a greet function that optionally accepts a title, like 'Dr.' or 'Ms.', and formats the greeting accordingly.

Your Task

  • Complete the function greet(name: string, title?: string): string.
  • If title is provided, return 'Hello, ' + title + ' ' + name.
  • Otherwise, return 'Hello, ' + name.
  • Do not log in this problem; return the string.

Examples

greet('Alex') // 'Hello, Alex' greet('River', 'Dr.') // 'Hello, Dr. River'