Help Need: HTML Canvas dynamic chess board; Algorithm problem

So, I’m learning image manipulation with canvas, so I decided to make a simple chess board with simple scalping dynamics. There seems to be a logical error somewhere cause this is the result of the code provided after image:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here