O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Silver Challenge

The error messages printed out by evaluate(_:) are useful, but not as useful as they could be. Here are a couple of erroneous inputs and the error messages they produce:

evaluate("1 + 3 + 7a + 8")
> Input contained an invalid character: a

evaluate("10 + 3 3 + 7")
> Invalid token during parsing: .number(3)

Make these messages more helpful by including the character position where the error occurred. After completing this challenge, you should see error messages like this:

evaluate("1 + 3 + 7a + 8")
> Input contained an invalid character at index 9: a

evaluate("10 + 3 3 + 7")
> Invalid token during parsing at index 7: 3

Hint: You will need to associate error positions with your existing error cases. To convert ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required