Answers for "how to reverse a string in javascript without using reverse method"

58

javascript reverse a string

function reverseString(s){
    return s.split("").reverse().join("");
}
reverseString("Hello");//"olleH"
Posted by: Guest on August-05-2019
2

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));
Posted by: Guest on February-08-2021
2

how to reverse a string in javascript without using reverse method

function reverseString(str) {
    return str.split("").reverse().join("");
}
reverseString("hello");
Posted by: Guest on May-13-2020
1

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');
Posted by: Guest on November-18-2020
2

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");
Posted by: Guest on September-03-2020
0

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");
Posted by: Guest on November-18-2020

Code answers related to "how to reverse a string in javascript without using reverse method"

Code answers related to "Javascript"

Browse Popular Code Answers by Language