DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Reverse Words in a String III

/**
 * @param {string} s
 * @return {string}
 */
var reverseWords = function(s) {
    let newArray =[];
    let arraySplit = s.split(' ').filter(str => str);

    for(let i=0; i<arraySplit.length; i++){
        let reverseStr =  arraySplit[i].split('').reverse().join('')
        newArray.push(reverseStr);
    };

    return newArray.join(" ");
};
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
frankwisniewski profile image
Frank Wisniewski • Edited
const reverseWords = s => [...s].reverse()
   .join('').split(' ').reverse().join(' ')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
elvissautet profile image
Elvis Sautet • Edited

simple & clean KUDOS

Collapse
 
zoppatorsk profile image
Zoppatorsk

I just wanted to try it and came up with this...

const reverseWords = (str) => str.split(' ').map((word) => word.split('').reverse().join('')).join(' ');
Enter fullscreen mode Exit fullscreen mode

Yeah, both ways work..