Check my code is it correct?

https://www.codepile.net/raw/9p6aDoQe.js

Yes it is fine, if you are facing any particular issue with it then tell.

As You have covered all the possible edges so it is correct.