mardi 26 décembre 2017

Insert random string into each instance of whitespace

I'm trying to insert a randomly selected string into each instance of whitespace within another string.

var boom = 'hey there buddy roe';
var space = ' ';
var words = ['cool','rad','tubular','woah', 'noice'];
var random_words = words[Math.floor(Math.random()*words.length)];

for(var i=0; i<boom.length; i++) {
  boom.split(' ').join(space + random_words + space);
}

Output comes to:

=> 'hey woah there woah buddy woah roe'

I am randomly selecting an item from the array, but it uses the same word for each instance of whitespace. I want a word randomly generated each time the loop encounters whitespace.

What I want is more like:

=> 'hey cool there noice buddy tubular roe'

Thanks for taking a look.

(This is beta for a Boomhauer twitter bot, excuse the variables / strings 😅)




Aucun commentaire:

Enregistrer un commentaire