2 cents: for reading pleasure, if you have a return in the middle of a function, put it at least at the beginning of the line - there it is a little bit more visible.
Besides that I also tend to "break out" of functions. Especially in cases like this - the "return -1" at the end clearly signifies a default value, so there has to be another return...
Nice observation. I added the checks for zeros.
Good catch. A test case should be added to check for zero values.
2 cents: for reading pleasure, if you have a return in the middle of a function, put it at least at the beginning of the line - there it is a little bit more visible.
Besides that I also tend to "break out" of functions. Especially in cases like this - the "return -1" at the end clearly signifies a default value, so there has to be another return...