I’m after some advice, whether i’m going down the right path. I wish to build sentences dynamically, which i want to change the wording based on a quantity value.
For example, the quantity value will equal the amount of people’s names shown.
So if quantity equals 1 it might be like this: “Paul goes down to the shop”
However if quantity equals 2, i want to change the sentence to: “Paul & Ringo both go down to the shop”
switching the word “GOES” to “BOTH GO”
Or perhaps if quantity equals 3, it might be: “Paul, Ringo & John all go down to the shop”
switching “GOES” to “ALL GO”
I’m not just looking at building 1 sentence, i’m trying to build several and then 1 is chosen at random and then displays based on pre-said quantity.
Currently, I have a json array of sentences, like the above.
And my current thought process is choosing a array item at random, and based on a random quantity, and by using some mapping and replace regex, for example:
`var str = “I have a cat, a dog, and a goat.”;
var mapObj = {cat:”dog”,dog:”goat”,goat:”cat”};
var re = new RegExp(Object.keys(mapObj).join(“|”),”gi”);
str = str.replace(re, function(matched){
return mapObj[matched];
});`
the string would be a sentence out of my json array, i welcome any thoughts?
Is there another way I can look at doing this? Or shall I continue?