+ 2
I want a input to return a random response from one of 3 set lines of text, how do i go about that ?
using JavaScript to process input and reply with at random one of these replys: . hi "input" how are you? . how was your day "input" ? . "input" whats you favorite color?
2 ответов
+ 2
var greets = [
"Hi {input} how are you?",
"How was your day {input} ?",
"{input}, whats you favorite color?"
]
var input = "Serena"
var index = ~~(Math.random() * 3);
var output = greets[index].replace(/{input}/,input);
console.log(output);
+ 1
Put the text where each line is an element of an array. Then pick a random number using `Math.random();`. Set the random number to a variable and output `array[thatnumberyoualreadygotfromrandom];`