lundi 25 mai 2020

Javascript random code for selecting array not working properly

function whosPaying(name) {
  var position = name.length;

  var randomPerson = Math.floor(Math.random() * position);

  console.log(name[randomPerson] + " is going to buy lunch.");
}


whosPaying("Jack", "Ben", "Jenny", "Michael", "Chloe");

When I run this code to get output in console log instead of selecting a name it selects one letter from the the first name (here:jack).




Aucun commentaire:

Enregistrer un commentaire