Random Text Display Code Help

Hi All!

Looking to be able to retrieve text in a random order in a text box upon a button click. I tried to use the following to no avail:

export function button1_click_1(event) {
//Add your code for this event here:

const ArrayList = [
‘clawed’,
‘floating’,
‘smooshed’,
‘asleep’
];

$w.onReady( function (){
const randomIndex = Math.floor(Math.random() * (ArrayList.length - 1));
$w(‘#button1’).link = ArrayList[randomIndex];
});

Any advice? Should I create a text box for each potential result in the list array instead?

Thanks for any guidance you might have!

1 Like

Your code has a number of issues.

  • The function button1_click_1() does not have a closing bracket. Or you tried to include the onReady() function in this function which is an error.

  • You are assigning a string from ArrayList to the button’s link property, and a string is not a link.
    Do you see error messages in the Developers console?

1 Like

I had the same problem but these issue fixes helped!

If you still have a problem, here is what I did for myself and it worked!
I used a box instead of a button though.

export function box1_click_1(event) {

 const ArrayList = [
  'clawed',  
  'floating', 
  'smooshed',
  'asleep',
 ];

 const randomIndex = Math.floor(Math.random() * (ArrayList.length));
    $w('#box1').text = ArrayList[randomIndex]
}

I’m not sure how it worked for you when a box doesn’t have text by default, but I tried this code on a basic text element and I had results.

export function text18_click(event) {

const ArrayList = [
‘clawed’,
‘floating’,
‘smooshed’,
‘asleep’,
];

const randomIndex = Math.floor(Math.random() * (ArrayList.length));
$w(‘#text18’).text = ArrayList[randomIndex]
}

1 Like

I… uh… somehow got it to work with a button. I don’t know how much help I can be, but here you go.

export function button3_click(event) {

const ArrayList = [
‘clawed’,
‘floating’,
‘smooshed’,
‘asleep’,
];

const randomIndex = Math.floor(Math.random() * (ArrayList.length));
$w(‘#text18’).text = ArrayList[randomIndex];
}

1 Like

I am having an issue with the export function (button1_click)… are you able to suggest a solution?

@tsuyoidesigns I had an issue with that, too. If you’re able, check the properties on the button you’re attempting to use for the code and manually add an onClick() action. When the code for that action pops up in the developer console, remove it entirely or replace the " export function button1_click(event)" action with it by copy-pasting it there.

1 Like