Dont understand why setInterval and setTimeout is not working as expected

Hello, I dont understand why my code is not working.
i have the code here -

it is beginning stage of a sorting visualizer, its not working as expected and i have wasted hours looking what i can do but still i dont understand whats wrong.

  1. i have a setInterval which mimics the for loop but on 1 sec interval
    (in each iteration 2 elements are compared)
  2. in each iteration a function is called which compares the two element while comparing it highlights the corresponding dom elements. (this is the part that is not working) . I have a setTimeout with x interval there so that it highlights the elements for x amount of time. But somehow different elements are selected

Please help, I have spent hours and hours of my time but still I just dont know