1

What I'm trying to do is basically take the below code and make it work so that I can combine items from several arrays into one sentence. So for instance, right now if I run this code it will give me random items from myarray, i.e. item1, 2, etc. But what I would like to do is create several arrays and have the result be a sentence which includes (in this order) random items from array1, array2, array3, etc. Basically, a random sentence generator using arrays. What differs between this and proposed duplicate item is that I already know how to randomize items within a single array, but would like to combine several array items into one sentence structure. (array1 item) + (array2 item) + (array3 item)

 function GetValue()
{
var myarray= new Array("item1","item2","item3");
var random = myarray[Math.floor(Math.random() * myarray.length)];
document.getElementById("message").innerHTML=random;
}

function GetValue()
{
    var myarray= new Array("item1","item2","item3");
    var random = myarray[Math.floor(Math.random() * myarray.length)];
   document.getElementById("message").innerHTML=random;
}
<input type="button" id="btnSearch" value="Search" onclick="GetValue();" />
<p id="message" ></>
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
pfbarnet
  • 101
  • 10
  • 2
    Possible duplicate of [How to randomize (shuffle) a JavaScript array?](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – nicael Jul 20 '16 at 20:41

3 Answers3

0

All you have to do is the create 2 more arrays and do the same thing to get a random word then concatenate them together. Like this:

function GetValue() {
  var myarray1 = new Array("item1", "item2", "item3");
  var myarray2 = new Array("item4", "item5", "item6");
  var myarray3 = new Array("item7", "item8", "item9");
  var random1 = myarray1[Math.floor(Math.random() * myarray1.length)];
  var random2 = myarray2[Math.floor(Math.random() * myarray2.length)];
  var random3 = myarray3[Math.floor(Math.random() * myarray3.length)];
  var output = random1 + ' ' + random2 + ' ' + random3;

  document.getElementById("message").innerHTML = output;
}
<input type="button" id="btnSearch" value="Search" onclick="GetValue();" />
<p id="message">
  </>
CascadiaJS
  • 2,320
  • 2
  • 26
  • 46
0

Use splice:

var myarray= new Array("item1","item2","item3");
while(myarray.length>0){
    var random = myarray.splice(Math.floor(Math.random() * myarray.length));
   document.getElementById("message").innerHTML += ' ' + random;
}

Here is the DEMO

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

Something like this?

var fragments = [];
fragments.push(["I","You","We","He","She"]);
fragments.push(["called","left","saw","loved","hated"]);
fragments.push(["the","a","some","another"]);
fragments.push(["store","person","rabbit","dog"]);

function getSentence() {
    var sentence = '';
    for (var i=0; i<fragments.length; i++) {
        sentence += fragments[i][Math.floor(Math.random() * fragments[i].length)] + ' ';
    }
    document.getElementById("message").innerHTML = (sentence.trim() + '.');
}
<input type="button" id="btnSearch" value="Search" onclick="getSentence();" />
<p id="message" ></>
Drey
  • 130
  • 9