The functions below share ~80% of their logic. Any change to the formatting must be made in 3 places.
// dirty
function formatUSD(amount) {
if (amount < 0) return '-$' + Math.abs(amount).toFixed(2);
return '$' + amount.toFixed(2);
}
function formatEUR(amount) {
if (amount < 0) return '-€' + Math.abs(amount).toFixed(2);
return '€' + amount.toFixed(2);
}
function formatGBP(amount) {
if (amount < 0) return '-£' + Math.abs(amount).toFixed(2);
return '£' + amount.toFixed(2);
}Refactor by extracting a generic formatCurrency(amount, symbol) helper. Then solve(amount, type) dispatches to the right formatter — 'formatUSD', 'formatEUR', or 'formatGBP'.
Sample tests