How do I use external javascript functions throughout my app

For simplicity's sake lets say I want to pull a random character from a specific list of characters.

var strings = ['a', 'b', 'c'];
var randomIndex = Math.floor(Math.random() * strings.length);
var randomString = strings[randomIndex];

I want everything to be inside of the function and just call get me a random character.

There seems to be suggestions that you need to put it in the preload, but how, I'm new to this and don't quite get the seperation.
Any help or pointer to a similiar implementation would be appreciated.

Hi @ADLADLADLAD,

You're on the right track, you can do this via preloaded JS. In your case, it would look something like this:

Navigate to the Preloaded JS section of your app along the side menu:
image

For your example, define a function like this:

window.getRandomString = () => {
  var strings = ['a', 'b', 'c'];
  var randomIndex = Math.floor(Math.random() * strings.length);
  var randomString = strings[randomIndex];
  return randomString
}

Now, anywhere in your app you need to use that function, you can simply call window.getRandomString(), so if it was a text box in your app, in the "value" section of the component you could put in I've chosen {{ window.getRandomString() }} for you!

Hi @ADLADLADLAD + @MikeCB,

I wanted wanted to point you both to a closed alpha we just launched for Sync Functions, which may be of interest to you. Let me know if you'd like to be added!

Best,
Erin

That would be great, please add our account to this Erin.