That is logically correct, but it doesn’t work here because of the way the test cases are written. They just have random values for Z1 and A1 in the input for the test case, so using the formula based on tanh(Z1) fails the test. I’ve filed a bug about this, but it has not been fixed yet.
This issue has come up before. Here’s a thread about it, which also points to this one.