If we want to build a phrase using random words of arrays, we could use
part_0=["Hello",....]
part_1=["I am",.....]
phrase=part_0[Math.round(Math.random() *(part_0.length-1))]+part_1[Math.round(Math.random() *
(part_1.length-1))]+...+part_n[Math.round(Math.random() *(part_n.length-1))]
But as you se above this will take too long for a n number of words, so instead of that fat part i tried to build the phrase with a for loop. The problem here is that in the for loop, part_i is read plain text, and not as part_number(i)
for (var _i = 0; _i < 3; _i++) {
phrase.add(part_i);
};
Any idea to solve this? Thanks
What you need here is an array of those arrays:
function pickRandom(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
let sections = [
["Hello,", "Hi,", "Howdy!", "Hey,"],
["I am", "You are", "They are", "We are"],
["making", "building", "crafting", "decorating"],
["a tower", "a machine", "a table", "a chair"],
["with", "using", "gluing", "consisting of"],
["toilet paper", "plastic cups", "paper clips", "staples"]
];
let phrase = sections.map(pickRandom).join(" ");
console.log(phrase);