Write a function that takes a sentence as input and reverses each word with an odd length while leaving words with an even length unchanged. The function should maintain the original word order and spacing.
Input: str = "This is a test sentence with odd and even length words"
Output: "This is a test sentence with ddo dna even length sdrow"
Input: str = "Hello world"
Output: "olleH dlrow"
The input string will not have leading or trailing spaces.
Words are defined as sequences of non-space characters separated by single spaces.
There will be no extra spaces between words in the input string.
function reverseOddLengthWordsUsingSplitMapJoin(sentence) {
return sentence
.split(" ")
.map(word => word.length % 2 === 1 ? word.split("").reverse().join("") : word)
.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingSplitMapJoin(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingSplitMapJoin(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingForLoop(sentence) {
const words = sentence.split(" ");
for (let i = 0; i < words.length; i++) {
if (words[i].length % 2 === 1) {
words[i] = words[i].split("").reverse().join("");
}
}
return words.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForLoop(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingForLoop(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingForOfLoop(sentence) {
const words = sentence.split(" ");
const reversedWords = [];
for (const word of words) {
if (word.length % 2 === 1) {
reversedWords.push(word.split("").reverse().join(""));
} else {
reversedWords.push(word);
}
}
return reversedWords.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForOfLoop(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingForOfLoop(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingReduce(sentence) {
return sentence
.split(" ")
.reduce((acc, word) => {
const reversedWord = word.length % 2 === 1 ? word.split("").reverse().join("") : word;
return acc + (acc ? " " : "") + reversedWord;
}, "");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingReduce(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingReduce(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingArrayFrom(sentence) {
return Array.from(sentence.split(" "))
.map(word => word.length % 2 === 1 ? Array.from(word).reverse().join("") : word)
.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingArrayFrom(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingArrayFrom(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingManualReversal(sentence) {
return sentence
.split(" ")
.map(word => {
if (word.length % 2 === 1) {
let reversedWord = "";
for (let i = word.length - 1; i >= 0; i--) {
reversedWord += word[i];
}
return reversedWord;
}
return word;
})
.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingManualReversal(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingManualReversal(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingReplace(sentence) {
return sentence.replace(/\S+/g, word => word.length % 2 === 1 ? word.split("").reverse().join("") : word);
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingReplace(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingReplace(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingMapSplitReverse(sentence) {
return sentence
.split(" ")
.map(word => word.length % 2 === 1 ? [...word].reverse().join("") : word)
.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingMapSplitReverse(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingMapSplitReverse(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingForEach(sentence) {
const words = sentence.split(" ");
words.forEach((word, index) => {
if (word.length % 2 === 1) {
words[index] = word.split("").reverse().join("");
}
});
return words.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingForEach(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingForEach(str2); //output: olleH dlrow
function reverseOddLengthWordsUsingLinkedList(sentence) {
const words = sentence.split(" ");
const reversedWords = [];
for (const word of words) {
if (word.length % 2 === 1) {
let reversedWord = "";
for (let i = word.length - 1; i >= 0; i--) {
reversedWord += word[i];
}
reversedWords.push(reversedWord);
} else {
reversedWords.push(word);
}
}
return reversedWords.join(" ");
}
const str1 = "This is a test sentence with odd and even length words";
reverseOddLengthWordsUsingLinkedList(str1); //output: This is a test sentence with ddo dna even length sdrow
const str2 = "Hello world";
reverseOddLengthWordsUsingLinkedList(str2); //output: olleH dlrow