How can I get following output when I pass following string in function as argument?
input: "Reverse this line"
output: "esreveR siht enil"
function reverseWords(string) {
var wordArray = string.split(" ");
var resultWordArray = [];
var requiredSentance;
wordArray.forEach(word => {
if (word == " ") {
var space = " ";
resultWordArray.push(space);
} else {
var SplittedWord = word.split("");
var reversedWordsLettersArray = [];
SplittedWord.forEach(letter => {
reversedWordsLettersArray.unshift(letter);
var reversedWord = reversedWordsLettersArray.join("");
resultWordArray.push(reversedWord);
})
}
})
var resultSentance = resultWordArray.join(" ");
console.log(resultSentance);
}
reverseWords("Reverse this line");
which is returning this as output:
"R eR veR eveR reveR sreveR esreveR t ht iht siht l il nil enil"
What am I missing here?
You can split the string into words and map through each word, splitting it into a character array and reverse the array, then join back the words into a single string. Like this:
string.split(' ').map(s => s.split('').reverse().join('')).join(' ')
To explain the code:
string.split(' ')
This line splits the whole string into array of words, "Reverse this line"
becomes ["Reverse", "this", "line"]
(note that the spaces are gone). Keep in mind that double spaces will be still split as a single space.
Since we now have an array, you can iterate the whole string using the .map
function. It works similarly to how you've tried to use the .forEach
, with the difference that the .map
function will return a new array with modified values which are returned in each iteration of the .map
callback function. Make sure to return something, or the values will be undefined
! The arrow function from ES2015+ has implicit return as you see in the example above, but if you were to use => {...}
, the block needs an explicit return
statement to work.
The code of the .map
function callback
s => s.split('').reverse().join('')
Receives a s
, which is a single word from the array above, splits the word by empty string, which is a workaround to split a string into characters. For example, "Reverse"
becomes ["R", "e", "v", "e", "r", "s", "e"]
. The .reverse()
function's name is quite descriptive, it just reverses the array. But be careful! It mutates the array, so if you call the .reverse()
function on a random array, it will be reversed from that line on. This could cause some issues if you're not careful. Lastly, the .join('')
puts the reversed array of characters back into a single string word.
And finally the .join(' ')
at the end puts the mapped array, which now contains reversed words, back together with spaces in-between, similarly to how .join('')
puts the array of characters back into a single word.
Using your code specifically, you are very close with what you are trying to do. You just need to move the declaration of the reversedWord
variable out of the forEach
loop, update inside the forEach
loop, which will eventually be defined with the entire reversed word.
function reverseWords(string) {
var wordArray = string.split(" ");
var resultWordArray = [];
var requiredSentance;
wordArray.forEach(word => {
if (word == " ") {
var space = " ";
resultWordArray.push(space);
} else {
var SplittedWord = word.split("");
var reversedWordsLettersArray = [];
var reversedWord; //define variable here
SplittedWord.forEach(letter => {
reversedWordsLettersArray.unshift(letter);
reversedWord = reversedWordsLettersArray.join("");
})
resultWordArray.push(reversedWord); // push it here
}
})
var resultSentance = resultWordArray.join(" ");
console.log(resultSentance);
}
reverseWords("Reverse this line");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With