Returning Early From a Function

The quote form on the translator’s site needs her rules in JavaScript, and the first rule to port is the turnaround. Bauer Legal has sent a 6,200-word contract it needs fast, and Café Linde wants its 180-word menu translated. Each reply has to promise a number of working days, and a rush job changes that number.

In the Python script, a small function calculates the number of days and returns early when the job is a rush. That function ports to JavaScript almost line for line. What changes is how JavaScript treats a call that gets something wrong.

Return Early From the Rush Branch

The turnaround rule is two working days plus one for every full 2,000 words. A rush job gets half of that, rounded down, but never less than one day. Here’s the Python helper, called for both clients:

Language: Python
def working_days(words, rush):
    days = 2 + words // 2000
    if rush:
        return max(days // 2, 1)
    return days

print(f"Bauer Legal: {working_days(6200, True)} working days")
print(f"Café Linde: {working_days(180, False)} working days")

In the JavaScript port, def becomes function, and the body sits in braces:

Language: JavaScript
function workingDays(words, rush) {
  const days = 2 + Math.floor(words / 2000);
  if (rush) {
    return Math.max(Math.floor(days / 2), 1);
  }
  return days;
}

console.log(`Bauer Legal: ${workingDays(6200, true)} working days`);
console.log(`Café Linde: ${workingDays(180, false)} working days`);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.