Target's State Check by Name or String

What would be the proper syntax if I wanted to check, within a damage formula, whether or not an enemy is affected by ANY state that has a certain string in the state's name?

I'm working on something where enemies can all apply a similar state on them, but each one varies slightly, so just checking for one ID won't cut it. However, all of these states would have an identical string in the state's name.

As far as I can tell, that information is retrieved with...

$dataStates[id].name

So I would assume something like...

Game_Battler.prototype.testingStringCheck = function(a, b) {
baseDamage = (a.atk * 4) - (b.def / 2);
modifiedDamage = 1;
if (b.isStateAffected($dataStates[id].name.contains('string'))) {
modifiedDamage *= 2;
}
finalDamage = baseDamage * modifiedDamage;
return finalDamage;
};

Just spitballing there, haven't tested, but is that anywhere in the ballpark of correct?
Could add a small Plugin that has:
Code:
Game_Battler.prototype.isNamedStateAffected = function(stateName) {
    return this.states().some(state => state.name.includes(stateName));
};
Then make use of the new method in your damage formula:
Code:
(a.atk * 4) - (b.def / 2) * (b.isNamedStateAffected('string') ? 2 : 1)

As an example:
Code:
$gameParty.members()[0].addState(8);
$gameParty.members()[0].states().some(state => state.name.includes('Confu')); // true
$gameParty.members()[0].states().some(state => state.name.includes('Poi')); // false

If you have an older version of MV that doesn't support ES6:
Code:
Game_Battler.prototype.isNamedStateAffected = function(stateName) {
    var states = this.states();
    for (let i = 0; i < states.length; i++) {
        if (states[i].name.indexOf(stateName) !== -1) {
            return true;
        }
    }
    return false;
};

If you don't want a Plugin:
Code:
(a.atk * 4) - (b.def * 2) * (b.states().some(state => state.name.includes('string')) ? 2 : 1);

If you have an older version of MV that doesn't support ES6, I'm not sure there's a non-obnoxious way to add the code in the formula and I'd recommend just going the small Plugin route.
Just spitballing there, haven't tested, but is that anywhere in the ballpark of correct?
Kind of, but not really. As written, there's no reason for it to be a method of Game_Battler, and it checks only one state using an id variable that's undefined.

What @Zevia provided with some() is all you need.

So you'd use b.states().some( etc. in your damage formula.
Just spitballing there, haven't tested, but is that anywhere in the ballpark of correct?

Zevia gave a solution, but to give the notes i was going to add:

in JS its includes(), not contains()

isStateAffected() takes an integer, not a boolean

you shouldnt use a function to calculate your damage the way you did way unless all your skills like that use the same formula and stats. itd be better to make it a separate function and use it to modify the formula with it than baking it in
Worked like a charm, just a quick question before this gets closed up, what does the => translate to in JS?
Really, the purpose of the lexical arrow is to preserve execution context for use of the keyword "this". In fact, the engine has quite a lot of code that needs to pointedly not use it because of that (usually when you're defining new prototype methods, it's important to use the function keyword instead).

In this case, it's just a bit of shorthand to replace the function expression because the execution context shouldn't matter in the way you're using the code.

As an example:
Code:
const someObj = {
  name: 'Some name',
  introduce: function() {
    console.log('My name is', this.name);
  }
};
someObj.introduce(); // Prints "My name is Some name"

const anotherObj = {
  name: 'Another name',
  introduce: () => console.log('My name is', this.name);
};
anotherObj.introduce(); // Depends on browser environment - running in Firefox
                        // prints "My name is ", some browsers will throw an error

If you're not running code that references the "this" keyword, lexical arrow functions just allow for some simpler syntax - for example, an arrow function without a code block is assumed to return whatever the line of code is, e.g.:
Code:
const helloWorld = () => 'Hello world';
console.log(helloWorld()); // Prints 'hello world'

const withCodeBlock = () => {
  'Hello world';
};
console.log(helloWorld()); // Prints undefined because the function didn't return anything

const withReturn = () => {
  return 'Hello world';
};
console.log(withReturn()); // Prints 'hello world'

Original thread: https://forums.rpgmakerweb.com/threads/targets-state-check-by-name-or-string.167423/