0

I have been creating a pokemon stat calculator using html, css and javascript for my personal use. The code mostly works and my issue is based specific part of it.

var Natures = { 'Adamant':{'pos':'Atk', 'neg':'SpA'} }

Above is a sample of an object that stores data on natures(each nature boosts one stat by 10% and reduces another by the same amount). The nature values are objects with properties that represent the stats they are boosting and reducing. My issue is with the following piece of code:

if(Natures.hasOwnProperty(nature)) {
    var plus = eval(Natures.nature['pos']);
    var minus = eval(Natures.nature['neg']);
    base.plus = Math.floor(base.plus*1.1);
    base.minus = Math.floor(base.minus*0.9);
}

The if statement is inside a function that modifies an object (base) and outputs its values (one for each of the 6 stats). The if statement checks the Natures object for a specific nature (a string given as a function parameter).

After some tests, I'm sure that the if statement is working. The issue lies with the next two lines. They are meant to access the pos and neg values for the nature and store them as plus and minus. The eval() is there so I can use plus and minus to access and modify the appropriate values of the base, which are not strings.

I've checked around and don't see any problems with my syntax. I'm also sure that I've isolated the problem in the code I've posted above. The function runs fine with nature isn't in Natures. Also, I've only been testing this in Chrome (Version 40.0.2214.115) so it might be the browser.

1 Answers1

0

A couple of problems:

First, change these lines

var plus = eval(Natures.nature['pos']);
var minus = eval(Natures.nature['neg']);
base.plus = Math.floor(base.plus*1.1);
base.minus = Math.floor(base.minus*0.9);

to

var plus = eval(Natures[nature]['pos']);
var minus = eval(Natures[nature]['neg']);
base[plus] = Math.floor(base[plus]*1.1);
base[minus] = Math.floor(base[minus]*0.9);

Secondly, eval('Atk') or eval('SpA') will not work unless these are defined somewhere else.

unobf
  • 7,158
  • 1
  • 23
  • 36
  • You've effectively answered my question. As a follow up does that mean that eval() can't be used to stand in for a specific type property? eval('Atk') was supposed to equal an non-string object key (used in multiple objects). I've been using object literal notation. Would that method have worked if I had used the object constructor method? – Vighnesh Raman Feb 26 '15 at 15:23
  • eval is evil. It is not good practice to use it. I cannot see from your example, why you would want to use it, why not simply put the values you need inside the object itself (maybe I am missing something because I have never been interested in Pokemon). – unobf Feb 26 '15 at 15:33
  • I'm not using eval for the part that I showed you anymore, but I needed to use it in another part of the program. The program calculates a pokemon's stats based on several factors inputted by the user. One factor is a pokemon's base stats, which I have stored as an object. The user selects the pokemon and the program takes the select option's value and uses it to call the appropriate object and get the base stats. The value was a string so I had to use eval to convert it to something I could use it to call the object. Here, I thought I could also use eval to call object key as well. – Vighnesh Raman Feb 26 '15 at 17:02