jeudi 30 août 2018

Javascript Random Name Always Returning Same item

I have this javascript code that collects the name from an array of string and return a random name, But everytime I fire the function, it is returning the same name; I need to refresh the web page to generate a new random name.

var items = ["Tony","Peter","Chris","Thor","Roger","Steve"];
var  randomItem = items[Math.floor(Math.random() * items.length)];

function getRandom(){
  document.getElementById("random").innerHTML = randomItem;
}
<button id="getRandom" onclick="getRandom()">Get Random name</button>
<span id="random">Click button to generate new name</span>



Aucun commentaire:

Enregistrer un commentaire