This can be simplified: `index` will be < 0 if i`sBool && stateVal`, so the ternary can be replaced with the second branch & we don't need a second if statement.