We are required to write a JavaScript function that takes in a string and returns a new string which has all the words reversed from the original string.
For example −
If the original string is −
"Hello World how is it outside"
Then the output should be −
"olleH dlroW woH si ti edistuo"
Now, let's write the code for this function −
Example
const str = 'Hello World how is it outside'; const reverseSentence = str => { const arr = str.split(" "); const reversed = arr.map(el => { return el.split('').reverse().join(""); }); return reversed.join(" "); }; console.log(reverseSentence(str));
Output
The output in the console will be −
olleH dlroW woh si ti edistuo