mardi 15 août 2017

Generate string with variable words in dynamic places in JavaScript

I saw Skype do this with their bots, their chat is randomized to make it look more human:

{Hi|Hey|Hello}, my name is a bot. {It's|It is} very {awesome|dope|cool} to meet you!

And the program replaces the {} parts with one random word from the ones specified, for example:

Hi, my name is a bot. It's very cool to meet you!

I was thinking about how this is possible, but other than putting every word into an array, running through all of them searching for "{" and "}" and after that replacing the string, I could not think about anything else.

You all are very creative and I'm sure there is another, better way of doing this.




Aucun commentaire:

Enregistrer un commentaire