I tried many times but failed until i saw this post:
It helped a lot.
And here’s the javascript version of it.
(Solution removed)