2

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 😅)

5 Answers 5

5

Maybe you can use regex instead however, you are not seeing the result you desire because you are randomly selecting one word and then replacing all occurrences of a space with it.

The regular expression below replaces occurrences of a space with a dynamic value returned by a callback. You could compare this callback to your for-loop but instead, it's iterating over the spaces found and by doing so you can replace each occurrence with a 'unique' random word.

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

let replace = boom.replace(/ /g, () => ` ${words[random()]} `);

console.log(replace);

Sign up to request clarification or add additional context in comments.

2 Comments

I think your answer is the one that works best, however explaining your code would be helpful, especially for OP and people not familiar with ES6.
Yep, just wanted to get my answer up first
0

The problem is, that random_words is set to a single word.

Try this instead:

var boom = 'hey there buddy roe';
var words = ['cool','rad','tubular','woah', 'noice'];

boom.replace(/ /g, (space)=> space + words[Math.floor(Math.random()*words.length)] + space);

Comments

0

To get the effect you desire, you need to do the word selecting inside the loop, not outside of it.

for(var i=0; i<boom.length; i++) {
    // get a new number every loop
    var random_words = words[Math.floor(Math.random()*words.length)];

    boom.split(' ').join(space + random_words + space);
}

Comments

-1

What is wrong with OP's code: random_words is initialized once only, with a random word. Intention there is, however, to select random word for every whitespace encountered instead.

You can either go with:

for(var i=0; i<boom.length; i++) {
   boom.split(' ').join(space + words[Math.floor(Math.random()*words.length)] + space);
}

... or make random_words a function that returns a random word, then call it in your 'boom' loop. With every call, a new word selection will occur.

Comments

-1

You need to recalculate the random word on each loop. Right now you have picked out a single random word, stored it in the random_words variable, and you reuse it each time. You could modify your code like this:

var boom = 'hey there buddy roe';
var space = ' ';
var words = ['cool','rad','tubular','woah', 'noice'];

function getRandomWord() {
  return words[Math.floor(Math.random()*words.length)];
}

// Uses the same because the value given to join is not recalculated each time:
console.log(boom.split(' ').join(space + getRandomWord() + space));

// You could do this with a standard for loop:
let result = "";
let split = boom.split(' ')
for(var i=0; i<split.length; i++) {
  result += split[i] + space;
  if (i === split.length - 1) break;
  result += getRandomWord() + space;
}

console.log(result);

// Or you can use a reduce:
let otherResult = boom.split(' ').reduce((res, word, index, split) => {
    if (index === split.length - 1) return res + space + word;
    return res + space + word + space + getRandomWord();
});

console.log(otherResult)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.