I'd like to write a code in javascript that selects a random word from a text, and replaces it for another word.
Here's my code:
var text = "dog cat apple stone";
var keyword = text[Math.floor(Math.random()*text.length)]; // select random word
var new_phrase = text.replace( keyword, "house"); // replace for other word
document.write("<p>" + text + "</p>" );
document.write("<p>" + new_phrase + "</p>");
However, this replaces a letter in the text not a word. Like this: "dog chouset apple stone"
How can I select a random word not a letter?