JSFiddle - React, Tailwind, and code Playground

by Kai

JavaScript

if (typeof Array.prototype.shuffle === 'undefined') {
    Array.prototype.shuffle = function () {
        var n = 0,
            k, tmp;

        for (; n < this.length - 1; n++) {
            k = n + Math.floor(Math.random() * (this.length - n));
            tmp = this[k];
            this[k] = this[n];
            this[n] = tmp;
        }
        
        return this;    
    };
}


function scramble (str) {
    var words = str.split(' '),
        tmp = [];
    
    if (words.length === 1) {
        return words[0].split('').shuffle().join('');
    }
    
    for (var i = 0; i < words.length; i++) {
        tmp.push(words[i].split('').shuffle().join(''));
    }
    
    return tmp.join(' ');
}