jeudi 1 août 2019

getting random element from array within a function

I am trying to print to console.log the chosen weapon from an array i have inside of a function. I am learning javascript

Ive tried function(weapon) { and ive also tried returning randWeapon, but not sure how to call the random element.

function randomWeapon() {
  let weaponArray = ["rock", "stick", "bat", "shoe"];
  return (rand = weaponArray[Math.floor(math.random() * weaponArray.length)]);
}
console.log(randomWeapon());

I want to either pass the weapon to another function or display in console.log(randWeapon)

error msg is : Uncaught ReferenceError: math is not defined




Aucun commentaire:

Enregistrer un commentaire