How to create a public function?

Hello,
I need a public function in JavaScript that can be reused by other functions.
1- Where do I create this function? For example the following function:

function calculateHours(startTime, endTime) {
    ...
    return hourDifference;
}

When I create a new function in Five, parameters five, context, result are added to the function. Can I simply get rid of these parameters and replace them by new parameters?

2- How can i call this public function? If I try:

function calculateHours(five, context, result)  {
  const start = five.field.StartIme;
  const end = five.field.EndTime;
  five.field.Hours = calculateHours(start, end);
  return five.success(result);
}

I have the error “calculateHours is not defined”

Thank you!

Hi Jean,

You can create public functions through ‘Libraries’ in Five.

  1. Click on ‘Logic’-> ‘Libraries

  2. Here create a new library and call it calculateHours.

  3. Click on ‘Click to edit’ and then add your code here that returns a value

  4. Click on Save when you are done with the code, now go to ‘Logic’-> ‘Functions

  5. Here, select the function where you would like to use the library (public function), then click on the ‘Libraries’ tab and add the ‘calculateHours’ library

  6. Now you should be able to use this library as a public function for this code. for eg:

    const a = calculateHours(start, end);
    console.log(a);

Ok it works. ! If I understand well, the Library tab for a given function is a kind of “import” for this function. Thanks!.