'Failed Collision detection function

I have made a game using p5.js. The player is the white square and the walls are blue. I have made a collison detection function, but it is pretty spotty. Sometimes, it works fine, but other times there is a slight distance between the player and the wall. Can you please tell me what is going wrong? Any help would be greatly appreciated! Code is below:

function collide(a, b, dx, dy){
  var newx = a.x + dx * playerSpeed;
  var newy = a.y + dy * playerSpeed;
  if (newx < b.x + b.size && newx + a.size > b.x && newy < b.y + b.size && newy + a.size > b.y){
    return true;
  }
  return false;
}

For full code go to https://replit.com/@TonyWu4/Project

If you want examples on what is happening, Links to screenshots are below.

Working Fine

Not working



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source