AniUI Academy
medium+250 XPPractice

Interpolate a mustache-style template

Email templates, i18n strings and CMS snippets all use some flavour of {{ placeholder }}. Writing the interpolator yourself is a five-minute job that teaches you exactly why the real ones are careful about missing keys.

Implement interpolate(template, data). Replace every placeholder of the form {{ path }} — where whitespace immediately inside the braces is ignored and path is made of letters, digits, _, $ and . — with the resolved value from data.

A path is resolved by splitting on . and walking data segment by segment, so user.name reads data.user.name and items.0.title reads data.items[0].title. If the walk hits null or undefined at any point, or the final value is null or undefined, the placeholder is replaced with the empty string.

Other values are inserted with String(value), so 0 renders as "0" and false as "false". Text that is not a complete placeholder — including a {{ with no closing }} — is left exactly as it is, and inserted values are never scanned for further placeholders.

What it has to do

  • Replace every {{ path }} placeholder, ignoring whitespace inside the braces.
  • Resolve dotted paths, including numeric segments that index arrays.
  • Replace a missing, null or undefined value with the empty string.
  • Insert other values with String(value), so 0 and false render as text.
  • Leave unclosed or malformed braces untouched and never re-interpolate inserted text.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.