JSFiddle - React, Tailwind, and code Playground

by Allie Yu

JavaScript

function permute(str){
    var newArr = [];
    
    if(str.length === 1) return [str];
    if(str.length === 2) return [str,str[1]+str[0]];
    
    str.split('').forEach(function(chr,idx,arr){
        var sub = .concat(arr);
        sub.splice(idx,1);
        permute(sub.join('')).forEach(function(p){
            newArr.push(chr + p);
        });
    });
    return newArr;
}
console.log(permute(1,2,3))