javascript - Use a variable in function argument -
what trying set variable specified in function something, see below if doesn't make sense.
function getrandomfromarray(arrayname,storevariable) { storevariable = arrayname[math.floor(math.random() * arrayname.length)] } var pettypearray = ['cat','dog','ferret','spider','companion bot']; getrandomfromarray(pettypearray,pettype)
for example, set pettype random string in pettypearray. doing right? able this?
you use return value of function:
function getrandomfromarray(arrayname) { return math.floor(math.random() * arrayname.length); } var pettypearray = ['cat', 'dog', 'ferret', 'spider', 'companion bot']; var randomelement = getrandomfromarray(pettypearray);
this makes code more readable. modifying parameter values inside function possible doesn't work primitive types such strings , integers. can modify properties of complex object.
Comments
Post a Comment