You would first need an array of possible responses. Something like this:
var responses = ["Well hello there!","Hello","Hola!"];
You can then use the Math.random
function. This function returns a decimal < 1, so you will need to convert it to an integer.
var responses = ["Well hello there!","Hello","Hola!"];
var responseIndex = Math.floor((Math.random() * 10) + 1);
Also, use the modulus (%
) operator to keep your random number within the confines of your array indexes:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
Finally, lookup your random response in the array:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
var response = responses[responseIndex];
alert(response);