Hey guys,
I’m pretty green on coding, but I have managed to make go through a bunch of forums to figure out how to make a random word generator using the code below. It works a charm, but I am hoping to find a way to make it not repeat any of the words in the array.
Here is the code:
export function button42_click_1 ( event ) {
const ArrayList = [
‘LAUGHING SO HARD YOU PEE’ ,
‘REVERSE PARALLEL PARKING’ ,
‘DUNGEONS AND DRAGONS’ ,
‘BORAT IMPERSONATIONS’ ,
‘EXISTENTIAL DREAD’ ,
‘STUBBING YOUR TOE’ ,
‘DOING THE WORM’ ,
‘THIRD WHEELING’ ,
‘JOINING A CULT’ ,
‘FLAT EARTHERS’ ,
‘DANCE BATTLES’ ,
‘FINGER GUNS’ ,
‘CAPITALISM’ ,
‘WITCHCRAFT’ ,
‘CATFISHING’ ,
‘CRAFT BEER’ ,
‘ASTROLOGY’ ,
‘COMMUNISM’ ,
‘STREAKING’ ,
‘FLIRTING’ ,
‘BAGPIPES’ ,
‘GODZILLA’ ,
‘PUBERTY’ ,
‘BIGFOOT’ ,
‘COOTIES’ ,
];
const randomIndex = Math . floor ( Math . random () * ( ArrayList . length ));
$w ( ‘#text48 ’ ). text = ArrayList [ randomIndex ];
}
Thanks in advance for any help you can give!
(Ignore the weird words hahaha)
J.D
December 11, 2022, 9:47pm
2
change the line before the end to be:
$w('#text48').text = ArrayList.splice(randomIndex,1)[0];
It should take the random word out of the array while assigning it to the text element;
Hey JD,
Thanks heaps for the response. It definitely seems like it’s repeating less, which is great! But it still seems like it’s repeating.
I am using both:
export function button41_click ( event ) {
const ArrayList =
[
‘1’ ,
‘2’ ,
‘3’ ,
‘4’ ,
‘5’ ,
];
**const** randomIndex = Math . floor ( Math . random () * ( ArrayList . length ));
$w ( '#text48' ). text = ArrayList [ randomIndex ];
$w ( '#text48' ). text = ArrayList . splice ( randomIndex , 1 )[ 0 ];
}
and
export function button41_click ( event ) {
const ArrayList =
[
‘1’ ,
‘2’ ,
‘3’ ,
‘4’ ,
‘5’ ,
];
**const** randomIndex = Math . floor ( Math . random () * ( ArrayList . length ));
$w ( '#text48' ). text = ArrayList . splice ( randomIndex , 1 )[ 0 ];
}
In my testing and it doesn’t seem to be doing anything different.
Thanks again for any help you can give.
J.D
December 12, 2022, 12:54am
4
Yes. I had to mention it. Put the array declaration itself in the global scope (outside the onClick function).
Mate, you are an absolute wizard! Thank you so much!