refactored if/else statement in indexOf method