dimanche 9 octobre 2016

How to generate 3 names without the same name repeated

window.onload = start;

function start () {
  var name = ["Hans","Ole","Nils","Olav","Per","Knut","Kari","Line","Pia"]
  var random = Math.floor(Math.random()*8)
  var random2 = Math.floor(Math.random()*8)
  var random3 = Math.floor(Math.random()*8)
  var name2 = []
  name2.push(name[random])
  name2.push(name[random2])
  name2.push(name[random3])

  for(i=0; i<3; i++) {
  document.getElementById("par").innerHTML += name2[i] + "<br/>" ;
  }
}

So far with this code i can generate 3 random names from the name array. But i want the 3 names not to be repeated, and I don't know how to make that happen.




Aucun commentaire:

Enregistrer un commentaire