0

I am making a new video game and I need help with a purchase feature. I want to use local variables to "create" a global variable. For example:

function example(apple,banana) {
    return unicorn.apple.banana.text
}
example(taco,burrito);

should return whatever unicorn.taco.burrito.text equals.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
EX master
  • 19
  • 1

1 Answers1

4

For this you need to use the Array Notation.

function example(apple, banana) {
    return unicorn[apple][banana].text
}
example(taco, burrito);

Because you need to replace apple and banana, with their values.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • Did not work out so how about I show you the **real** code. `frac.percent.buy = function (shop,purchase) { if (frac.buckaroonies >= frac[shop].shopcost[purchase].cost) { frac[shop].num = frac[shop].shopcost[purchase].addition + frac[shop].num; frac.buckaroonies = frac.buckaroonies - frac[shop].shopcost[purchase].cost; } };` – EX master Apr 27 '16 at 23:26
  • I try `frac.percent.buy(percent,one);` and I'm left with: `one is not defined – EX master Apr 27 '16 at 23:28
  • @EXmaster Show your full code. – Praveen Kumar Purushothaman Apr 27 '16 at 23:30