0

How can i make so the if else if statement would work as supposed? in this case thisminion.optiontable[option] would be the same call as thisminion.attack

i do have the following:

var option = 1;

 var optiontable = {1: 'attack', 2: 'defence', 3: 'intelligence'};
            var $result = [];
            var nameoption = optiontable[option];

 if (thisminion.optiontable[option] <= 100) {
                increasement = 10;
            } else if (thisminion.optiontable[option] >= 100 && thisminion.optiontable[option] < 200) {
                increasement = 5;
            } else if (thisminion.optiontable[option] >= 200 && thisminion.optiontable[option] < 300) {
                increasement = 5;
            } else if (thisminion.optiontable[option] >= 300 && thisminion.optiontable[option] < 400) {
                increasement = 1;
            } else {
                increasement = 0;
            }
maria
  • 207
  • 5
  • 22
  • 56

1 Answers1

1

If I'm reading your question correctly, you use brackets notation with optiontable[option] as the operand:

if (thisminion[optiontable[option]] <= 100) {

That gets the value of optiontable[option] ('attack') and uses it to look up the attack property of thisminion.

This is exactly the same thing you're doing when looking up optiontable[option]; using the value of option to look up a property on optiontable with that name (1, in that case).

Example:

var option = 1;

var optiontable = {1: 'attack', 2: 'defence', 3: 'intelligence'};

var thisminion = {
  attack: 75,
  defence: 100,
  intelligence: 25
};

console.log(thisminion[optiontable[option]]);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875