function replaceRandomWord(text: string, replacementWords: string[]): string {
const words: string[] = text.split(" ");
if (words.length === 0 || replacementWords.length === 0) {
return text; // nothing to do
}
// Pick random index in the sentence
const idx: number = Math.floor(Math.random() * words.length);
// Pick random replacement word
const newWord: string = replacementWords[Math.floor(Math.random() * replacementWords.length)];
// Replace it
words[idx] = newWord;
// Rebuild the string
return words.join(" ");
}
const text = "The quick brown fox jumps over the lazy dog";
const replacementWords: string[] = ["c#", "c++", "java", "rust", "python"];
const result: string = replaceRandomWord(text, replacementWords);
console.log(result);
/*
run:
"The quick brown fox c# over the lazy dog"
*/