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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -