From b7685057d27fdebec0fd7a70bc5886895f6bb6d6 Mon Sep 17 00:00:00 2001 From: zongyanqi Date: Mon, 20 Mar 2017 08:50:00 +0800 Subject: [PATCH] add Medium_151_Reverse_Words_in_a_String --- Medium_151_Reverse_Words_in_a_String.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Medium_151_Reverse_Words_in_a_String.js diff --git a/Medium_151_Reverse_Words_in_a_String.js b/Medium_151_Reverse_Words_in_a_String.js new file mode 100644 index 0000000..1f075af --- /dev/null +++ b/Medium_151_Reverse_Words_in_a_String.js @@ -0,0 +1,18 @@ +/** + * Given an input string, reverse the string word by word. + + For example, + Given s = "the sky is blue", + return "blue is sky the". + */ + +/** + * @param {string} str + * @returns {string} + */ +var reverseWords = function (str) { + return str.split(' ') + .filter(w => w) + .reverse() + .join(' '); +}; \ No newline at end of file