Javascript String oddwords()

Description

Javascript String oddwords()


String.prototype.oddwords = function() {
    var words = this.split(' ');
    var result = [];
    /*  w w  w.  j a  va 2  s. c o m*/
    for (var i = 0; i < words.length; i++) {
        var odd = (i % 2 != 0);
        var word = words[i];
        
        if (odd) {
            result.push(word.split('').reverse().join(''));
        } else {
            result.push(word);
        }
    }  
    
    return result.join(' ');
};



PreviousNext

Related