The translator’s two formatters are arrows now: forEmail turns a fee into a euro amount for a reply, and forSheet turns it into a plain number for her bookkeeping sheet. In her Python script, whoever asks for a quote line hands a formatter in.
That works because Python functions are values you can pass around. JavaScript functions are too, and JavaScript code leans on that far more. One Python habit doesn’t come along, though, and you’ll meet it at the end.
Hand a Formatter to the Quote Line
In the Python script, which still has quote() from earlier, quote_line() takes the formatter as a parameter and calls it on the fee. Email is the default, and the Nordwind GmbH line passes for_sheet because it goes into the spreadsheet:
def for_email(amount):
return f"€{amount:,.2f}"
def for_sheet(amount):
return f"{amount:.2f}"
def quote_line(client, words, fmt=for_email):
fee, days = quote(words)
return f"{client}: {fmt(fee)}, {days} working days"
print(quote_line("Café Linde", 180))
print(quote_line("Nordwind GmbH", 9400, for_sheet))
The JavaScript port keeps that shape. Here it is with quote() as you left it, returning the fee and the turnaround in an array:
function quote(words, rate = 0.12, rush = false, minimum = 40) {
const fee = Math.max(words * rate, minimum);
const days = 2 + Math.floor(words / 2000);
if (rush) {
return [fee * 1.5, Math.max(Math.floor(days / 2), 1)];
}
return [fee, days];
}
const euroFormat = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "EUR",
});
const forEmail = (amount) => euroFormat.format(amount);
const forSheet = (amount) => amount.toFixed(2);
function quoteLine(client, words, format = forEmail) {
const [fee, days] = quote(words);
return `${client}: ${format(fee)}, ${days} working days`;
}
console.log(quoteLine("Café Linde", 180));
console.log(quoteLine("Nordwind GmbH", 9400, forSheet));
Run it, and both lines match the Python script’s output:
Café Linde: €40.00, 2 working days
Nordwind GmbH: 1128.00, 6 working days
As in Python, a function stored in a const is a value like any other, so it can be a default, an argument, or something you call through a parameter.