javascript - functions return add to variable -
i have array of prices items
var st = [0, 1, 2, 0, 3, 0, 4, 0, 0, 0, 0, 0]
to figure out how money player gets runs function
function buy(a) { money += st[return a;] }
this should make
function buy(a) { money += st[1] }
the button pressed run buy() this
<button onclick='buy(1)'>sell</button>
when press button should return 1 , therefore add $1 players account getting , error
unexpected keyword 'return'
if need more information happy supply it
fidde if need it
as error message tells you, return
has no place you've put it. you've put statement (return a;
) inside property accessor (st[...]
). isn't valid.
from way you're using buy
, don't want return
@ all:
function buy(a) { money += st[a]; }
but if did want return value, you'd use return
separately. instance, if wanted return updated value of money
:
function buy(a) { money += st[a]; return money; }
(technically, return money += st[a];
return updated value of money
, i'm showing them separately emphasis , clarity.)
or if wanted return a
reason:
function buy(a) { money += st[a]; return a; }
Comments
Post a Comment