refactor solution with ternary operator to satisfy test where negative index is out of bounds