Write a function that takes a sentence as input and reverses each word within the sentence individually while maintaining the original word order and spacing.
Input: str = "Hey Aman is that correct way to reverse words of a string"
Output: "yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts"
function reverseEachWordUsingSplitMapJoin(sentence) {
return sentence.split(" ")
.map(word => word.split("").reverse().join(""))
.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitMapJoin(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingForLoop(sentence) {
let words = sentence.split(" ");
for (let i = 0; i < words.length; i++) {
words[i] = words[i].split("").reverse().join("");
}
return words.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingForLoop(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingForOfLoop(sentence) {
const words = sentence.split(" ");
const reversedWords = [];
for (const word of words) {
reversedWords.push(word.split("").reverse().join(""));
}
return reversedWords.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingForOfLoop(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingReduce(sentence) {
return sentence.split(" ")
.reduce((acc, word) => acc + (acc ? " " : "") + word.split("").reverse().join(""), "");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingReduce(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingArrayFromForEach(sentence) {
return sentence
.split(" ")
.map((word) => Array.from(word).reverse().join(""))
.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingArrayFromForEach(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingMapSplitReverse(sentence) {
return sentence
.split(" ")
.map((word) => [...word].reverse().join(""))
.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingMapSplitReverse(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingSplitReverseReduce(sentence) {
return sentence.split(" ")
.map(word => word.split("").reverse().join(""))
.reduce((acc, word) => acc + (acc ? " " : "") + word, "");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitReverseReduce(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingSplitManual(sentence) {
return sentence.split(" ")
.map(word => {
let reversedWord = "";
for (let i = word.length - 1; i >= 0; i--) {
reversedWord += word[i];
}
return reversedWord;
})
.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitManual(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingReplace(sentence) {
return sentence.replace(/\S+/g, word => word.split("").reverse().join(""));
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingReplace(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts
function reverseEachWordUsingSplitForJoin(sentence) {
const words = sentence.split(" ");
const reversedWords = [];
for (let i = 0; i < words.length; i++) {
let reversedWord = "";
for (let j = words[i].length - 1; j >= 0; j--) {
reversedWord += words[i][j];
}
reversedWords.push(reversedWord);
}
return reversedWords.join(" ");
}
const str1 = "Hey Aman is that correct way to reverse words of a string";
reverseEachWordUsingSplitForJoin(str1); //output: yeH namA si taht tcerroc yaw ot esrever sdrow fo a gnirts