How to debug or see the errors
If you are getting Type error undefined most often it is the variable its referring to. It is either not storing a value or is not declared as var.
User input validation
If your if condition variable is not matching as expected
Then make sure your variable’s type is correct. That is sometimes you might be matching a string but you might be storing it as a integer/float. Just print the type of the variable to see what is happening.