function reverseInPlace[str] {
var words = [];
words = str.split["\s+"];
var result = "";
for [var i = 0; i < words.length; i++] {
return result += words[i].split[''].reverse[].join[''];
}
}
console.log[reverseInPlace["abd fhe kdj"]]
What I expect is dba ehf jdk
, while what I'm getting here is jdk fhe dba
. What's the problem?
John Kugelman
337k66 gold badges509 silver badges559 bronze badges
asked Mar 19, 2018 at 10:26
5
This function should work for you:
function myFunction[string] {
return string.split[""].reverse[].join[""].split[" "].reverse[].join[" "]
};
allo
3,7546 gold badges36 silver badges66 bronze badges
answered Mar 19, 2018 at 10:42
you need to split the string by space
function reverseInPlace[str] {
var words = [];
words = str.match[/\S+/g];
var result = "";
for [var i = 0; i < words.length; i++] {
result += words[i].split[''].reverse[].join[''] + " ";
}
return result
}
console.log[reverseInPlace["abd fhe kdj"]]
answered Mar 19, 2018 at 10:31
AzadAzad
4,9863 gold badges27 silver badges53 bronze badges
3
Split
the string into words first before reversing the individual words
var input = "abd fhe kdj";
var output = input.split[ " " ].map[ //split into words and iterate via map
s => s.split[""].reverse[].join[ "" ] //split individual words into characters and then reverse the array of character and join it back
].join[ " " ]; //join the individual words
answered Mar 19, 2018 at 10:33
gurvinder372gurvinder372
65.2k9 gold badges69 silver badges90 bronze badges
const reverseWordIntoString = str => str.split[" "].map[word => word.split[""].reverse[].join['']].join[" "]
const longString = "My name is Vivekanand Panda";
const sentence = "I love to code";
const output = {
[longString]: reverseWordIntoString[longString],
[sentence]: reverseWordIntoString[sentence]
}
console.log[output];
answered Sep 8, 2021 at 10:39
You can make use of split
and map
function to create the reverse words.
You need to first split the sentence
using space
and then you can just reverse each word and join the reversed words again.
function reverseWord [sentence] {
return sentence.split[' '].map[function[word] {
return word.split[''].reverse[].join[''];
}].join[' '];
}
console.log[reverseWord["abd fhe kdj"]];
answered Mar 19, 2018 at 10:36
If you are looking like this o/p : "Javascript is Best" => "Best is Javascript"
Then can be done by simple logic
//sample string
let str1 = "Javascript is Best";
//breaking into array
let str1WordArr = str1.split[" "];
//temp array to hold the reverse string
let reverseWord=[];
//can iterate the loop backward
for[let i=[str1WordArr.length]-1;i>=0;i--]
{
//pushing the reverse of words into new array
reverseWord.push[str1WordArr[i]];
}
//join the words array
console.log[reverseWord.join[" "]]; //Best is Javascript
answered May 2, 2020 at 10:06
This solution preserves the whitespaces characters space ␣
, the tab \t
, the new line \n
and the carriage return \r
] and preserves their order too [not reversed] :
const sentence = "abd\t fhe kdj";
function reverseWords[sentence] {
return sentence
.split[/[\s+]/]
.map[word => /^\s+$/.test[word] ? word : word.split[''].reverse[].join['']]
.join[''];
}
console.log[reverseWords[sentence]];
answered May 2, 2020 at 10:45
eroakeroak
1,00710 silver badges17 bronze badges
If you want to reverse a string by word:
Example: 'Welcome to JavaScript' to 'JavaScript to Welcome'
You can also do something like:
var str = 'Welcome to JavaScript'
function reverseByWord[s]{
return s.split[" "].reverse[].join[" "];
}
// Note: There is space in split[] and Join[] method
reverseByWord[str]
// output will be - JavaScript to Welcome
answered Feb 24, 2021 at 7:14
pradeepkspradeepks
531 silver badge6 bronze badges
You can easily achieve that by the following code
function reverseWords[str] {
// Go for it
let reversed;
let newArray=[];
reversed = str.split[" "];
for[var i = 0;iword.split[""].reverse[].join[""]].join[" "] return rev } console.log[reverse["soumya prakash"]];//o/p=>aymuos hsakarp
answered Sep 12 at 7:23
1
How do you reverse text in JavaScript?
The split[] method splits a String object into an array of string by separating the string into sub strings.. The reverse[] method reverses an array in place. ... . The join[] method joins all elements of an array into a string..
How do I reverse a specific word in a string?
We can reverse each word of a string by the help of reverse[], split[] and substring[] methods. By using reverse[] method of StringBuilder class, we can reverse given string. By the help of split["\\s"] method, we can get all words in an array.
How do you reverse words in a given sentence without using any library method JavaScript?
“How can I reverse words in a string without using a built-in function in JavaScript?” Code Answer. function reverse[str]{. let reversed = "";. for [var i = str. length - 1; i >= 0; i--]{. reversed += str[i];. return reversed;.
How do I reverse a line in a string?
Strings can be reversed using slicing. To reverse a string, we simply create a slice that starts with the length of the string, and ends at index 0. The slice statement means start at string length, end at position 0, move with the step -1 [or one step backward].