Odering a list of integers in javascript?

arr is an array of integers. There are no repeating integers in arr. A function f(x,y) acting on two elements in the list moves x to y’s position and y to x’s position. Find out the least number of times you can call f so that the resulting array is in ascending order. Input Format
The first line of input is an integer n which Is the length of the array The second line is a list of space-separated integers. Input Constraints
There will be at least one integer in the array. Output Format
An integer which represents the minimum number of swaps to be made. Using javascript. Example, input 4 2854 output 1. If we swap 8 with 4, the array is sorted. so the output is 1

`function solution(n, arr) {
  let swaps = 0;
  let visited = new Array(n).fill(false);

  for (let i = 0; i < n; i++) {
    if (visited[i] || arr[i] === i + 1) continue;

    let cycleSize = 0;
    let j = i;

    while (!visited[j]) {
      visited[j] = true;
      cycleSize++;
      j = arr[j] - 1;
    }

    swaps += (cycleSize - 1);
  }

  return swaps;
}
`your text``