Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse all the words of sentence JavaScript
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
Advertisements