I want to reverse a string without using any inbuilt function, and I want it to change the original string itself but it doesn't work well. The language is Javascript. Show
asked Aug 15, 2017 at 10:28
3 Here is the simplest way to do without using any javascript inbuilt function.
answered Nov 3, 2020 at 22:00
Create a new string and add all the chars from the original string to it backwards:
Then just say:
answered Aug 15, 2017 at 10:40
yoav sarfatyyoav sarfaty 431 gold badge1 silver badge8 bronze badges Javascript strings are immutable, you cannot simply replace a character with another one
You can always create a new string and return it though answered Aug 15, 2017 at 10:36
marvel308marvel308 10.1k1 gold badge20 silver badges32 bronze badges Reverse the
answered Aug 15, 2017 at 10:32
prasanthprasanth 21.6k4 gold badges27 silver badges50 bronze badges 2 Well if you don't want to use the inbuilt functions here you go
answered Aug 15, 2017 at 10:42
Satyam PathakSatyam Pathak 6,1003 gold badges23 silver badges50 bronze badges
answered Apr 26, 2021 at 11:16
mohd jagirmohd jagir 711 silver badge3 bronze badges I think using below way is simple and clean. function reverse(str) {
} answered Dec 12, 2021 at 10:50
1 I think this question better answer is here. why char can't be change in a position. Duplicate
answered Aug 15, 2017 at 10:32
atiq1589atiq1589 2,1211 gold badge15 silver badges24 bronze badges 5 How do you reverse a sentence in JavaScript?JavaScript Algorithm: Reverse Words. let reverseWordArr = str.split(" ") Since we want to create another array but containing all the words in the string reversed, we will use the map method.. . map(word => word. split(""). ... . let reverseWordArr = str. split(" "). ... . return reverseWordArr.join(" "); Here is the full function:. How can I reverse a string without any inbuilt function?function reverse1(str){ var a = ""; for(var i = 0; i <= str. length/2; i++){ a = str[i]; str[i] = str[str. length-i-1]; str[str. length-i-1] = a; } return str; } var str = "abcdef"; reverse1(str);
How do I reverse a string without string builder?Just use a single array, with two index variables, and swap the first and the last characters, then swap the second and the next to last characters, etc... You keep doing that until your two indexes meet in the middle.
Is there any reverse function in JavaScript?JavaScript Array reverse()
The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.
|