Skip to main content
✂️ Text & String Tools

What you're building

A fillTemplate(template, data) function that replaces {{key}} placeholders in a template string with corresponding values from a data object.

Requirements

  • Placeholders are written as {{key}} (double curly braces, any key name).
  • Replace every placeholder whose key exists in data; leave unknown placeholders as-is (do not delete them).
  • The same placeholder may appear multiple times; replace all occurrences.
  • Whitespace inside the braces should be stripped: {{ name }} and {{name}} are both valid and refer to the same key.
  • Values in data are always strings or numbers; coerce them with String().

Example:

fillTemplate("Hi {{name}}, your order #{{id}} is ready!", { name: "Alex", id: 42 })
// → "Hi Alex, your order #42 is ready!"

Stretch goals

  • Support dot-notation keys like {{user.name}} for nested objects.
  • Add a strict mode that throws when an unknown key is encountered.

What this proves

You can use regex with a replacer function, handle dynamic key lookups, and build an extensible utility — the same pattern used by Handlebars, Mustache, and every notification pipeline.

⚡ 50 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
replaces known placeholders
leaves unknown placeholders intact
handles whitespace inside braces
replaces same key multiple times

Run your code to check these.