javascript reverse a string
function reverseString(s){
return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"
javascript reverse a string
function reverseString(s){
return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"
reverse a string javascript
//with built-in method
let string = "We are going to reverse a string with built-in method.";
let reversed = string.split("").reverse().join("");
console.log(reversed);
//with the for loop
let string = "We are going to reverse a string with the for loop.";
function reverse (string){
let reversed = "";
for(let char of string){
reversed = char + reversed;
}
return reversed;
}
console.log(reverse(string));
//with the spread operator
let string = "We are going to reverse a string with the spread operator.";
let reversed = [...string].reverse().join("");
console.log(reversed);
//with iteration
let string = "We are going to reverse a string with iteration.";
function reverse(string) {
for (var reversed = "", i = string.length - 1; i >= 0; i--) {
reversed += string[i];
}
return reversed;
console.log(reverse(string));
how to reverse a string in javascript without using reverse method
function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("hello");
how to reverse a string in javascript without using reverse method
function reverseString(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
reverseString('hello');
how to reverse a string in javascript without using reverse method
function reverseString(str) {
if (str === "")
return "";
else
return reverseString(str.substr(1)) + str.charAt(0);
}
reverseString("hello");
how to reverse a string in javascript without using reverse method
function reverseString(str) {
if (str === "") // This is the terminal case that will end the recursion
return "";
else
return reverseString(str.substr(1)) + str.charAt(0);
/*
First Part of the recursion method
You need to remember that you won’t have just one call, you’ll have several nested calls
Each call: str === "?" reverseString(str.subst(1)) + str.charAt(0)
1st call – reverseString("Hello") will return reverseString("ello") + "h"
2nd call – reverseString("ello") will return reverseString("llo") + "e"
3rd call – reverseString("llo") will return reverseString("lo") + "l"
4th call – reverseString("lo") will return reverseString("o") + "l"
5th call – reverseString("o") will return reverseString("") + "o"
Second part of the recursion method
The method hits the if condition and the most highly nested call returns immediately
5th call will return reverseString("") + "o" = "o"
4th call will return reverseString("o") + "l" = "o" + "l"
3rd call will return reverseString("lo") + "l" = "o" + "l" + "l"
2nd call will return reverserString("llo") + "e" = "o" + "l" + "l" + "e"
1st call will return reverserString("ello") + "h" = "o" + "l" + "l" + "e" + "h"
*/
}
reverseString("hello");
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us