Given a string, reverse each word in the sentence using javascript code example
Example 1: reverse every word in a string javascript
function reverseWords(str) {
const allWords = str.split(" ")
return allWords.map(item => item.split("").reverse().join("")).join(" ")
}
function reverseWords(str) {
return str
.split(" ")
.map(word => {
return word.split("").reverse().join("")
})
.join(" ")
}
var reverseWords = s => s.replace(/\S+/g, v => [...v].reverse().join``)
Example 2: reverse a word javascript
function reverseWords(str) {
const allWords = str.split(" ")
return allWords.map(item => item.split("").reverse().join("")).join(" ")
}