Putting a point to the nearest grid intersection

Hi im trying to make a grid with 10 columns and 10 rows and when mouse pressed a circle is placed to the nearest grid intersection. any idea how i can do this using arrays and loops to compare the distance between each circle and grid intersection?

Do you really need to calculate the distance or would it be enough just to round the value to your grid size? Something like this?

let gridX = round(mouseX / gridSize) * gridSize;
let gridY = round(mouseY / gridSize) * gridSize;

Here is an example:
https://editor.p5js.org/zuvala/sketches/wU5fBq_n1

If this simple solution doesn’t help, look into vectors. There is a function to calculate the distance between two points.
https://p5js.org/reference/#/p5.Vector/dist