JSFiddle - React, Tailwind, and code Playground

by russau

JavaScript

function perms(word) {
    var permutations = [];

    if (word == null) return null;

    if (word.length == 0) {
        permutations.push('');
        return permutations;
    }

    var first = word.substr(0, 1);
    var remainder = word.substr(1, word.length - 1);

    var words = perms(remainder);
    
    for (w in words)
    {
        for(var i =0; i<=w.length; i++)
        {
            x = w.substring(0, i) + first + x.substring(i,x.length());
            permutations.push(w);
        }
    }
    return permutations;
}

//var p = perms('abc');
//document.write(p);