cf.completefrontend

Write the codeEasy

Type formatPrice(amount, currency, decimals) so only amount is required.

Under strict, this function doesn't compile: every parameter is implicitly any. Type it so amount is a required number, currency is an optional string that defaults to "$", and decimals is an optional number that defaults to 2. The function returns a string.

Write this
function formatPrice(amount, currency, decimals) {  return `${currency ?? "$"}${amount.toFixed(decimals ?? 2)}`;} // formatPrice(5);          // "$5.00"// formatPrice(5, "€");     // "€5.00"// formatPrice(5, "€", 0);  // "€5"// formatPrice("5");        // should be a type error

Solve it in the editor

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…