Full permutation is realized by JS

function permutations(string) {
  return (string.length == 1) ? [string] : string.split("").map(
     (e, i) => permutations(string.slice(0,i) + string.slice(i+1)).map((e2) => e+e2)
  ).reduce((r,e) => r.concat(e)).sort().filter((e,i,a) => (i==0) || a[i-1] != e);
}

is there any boss who can order it? I don"t understand this code

Jun.21,2022

first of all, you have to understand the functions of map reduce concat sort filter in the JS array. It is recommended that you check the MDN website

.

in addition, it is not recommended to use this style of code in the project, because the readability is too poor, almost zero, if others take over your project to see this kind of code, they will probably scold their mother.

it is recommended to write

in several steps.
function permutations(string) {
    if (string.length == 1) {
        return [string]
    } else {
        const arry = string.split('') 
        arry.map((e, i) => {
            let newString = string.slice(0,i) + string.slice(i+1)
            // 
            permutations(newString).map((e2) => e+e2)
        })
        .reduce((r,e) => r.concat(e))
        .sort()
        .filter((e,i,a) => (i==0) || a[i-1] != e)
    }
}

it's OK to change it like this. You should be able to see the idea of this code clearly. However, the performance of this function is too poor, it is not recommended to write this way.

Menu