I m studying functional programming with javascript and I m having some problems dealing with value permutations.
Actually, I have an array that looks like:
[2, 1]
And I need to get functionally, with no mutation:
[1, 2]
This way, I've written a permute function that uses some ES6 features to makes the job:
export function permute (arr, indiceX, indiceY) {
  const intermediateArray = [
    ...arr.slice(0, indiceX),
    arr[indiceY],
    ...arr.slice(indiceX + 1)
  ]
  console.log([
    ...intermediateArray.slice(0, indiceY),
    intermediateArray[indiceX],
    ...intermediateArray.slice(indiceY + 1)
  ]) // prints [1, 1]
  return [
    ...intermediateArray.slice(0, indiceY),
    intermediateArray[indiceX],
    ...intermediateArray.slice(indiceY + 1)
  ]
}
Using this function, I always get
[1, 1]
And I don't understand why, because I first add the indiceY value at the indiceX place, and makes the same stuff just after but for the other value..
Any idea of what I m doing wrong?
EDIT : Some precisions, it should permute two items of an array of length N, for example :
permute([1, 3, 2, 6], 0,2) // should return [2, 3, 1, 6]
EDIT 2 : I've published the solution on my github account
 
     
     
     
    