What is javascript?

► Play

Javascript is a scripting language, primarily used on the web. It is used to increase the HTML pages and is commonly and is commonly found in HTML code. Javascript is an interpreted language. Therefore, it doesn’t need to be compiled.

How to reverse a string in javascript?

► Play

There are three ways to reverse a string in javascript.

  • Reverse a string with a built-in function
  • Reverse a string with decrementing a For loop
  • Reverse a string with Recursion

Reverse a string with a built-in function

► Play

There is three methods to solve this solution 1st string. prototype. split method() 2nd  the array. prototype. reverse method() and 3rd array. prototype. join . method()

  • The split method splits a string object into an array of a string by separating the string into substrings.
  • The reverse reverses an array in a place. The first element of an array becomes the last element and the last element become the first element of an array.
  • The join function in JavaScript joins all the elements in a string.               

Example to reverse a string with built-in function

► Play

Function reversestring ( str )

                 {

                        // Step 1. Use the split method()

                        Var splitstring = str . split ( “ “ ) ;

                        // Step 2. Use the reverse method()

                          Var reversearray = splitstring . reverse() ;

                       // Step 3. Use the join method()

                        Var joinarray = reversearray . join(“ ‘’) ;

                      // step 4. Return the reverse string

                        Return joinarray ;

                  }

         Reversestring ( “ string ” ) ;

Reverse a string with decrementing a for loop

► Play

Example program

Function reversestring ( str ) 

{

                                               // create an empty string

                                               Var newstring = “ “;

                                               // create the for loop

                                               For ( var J = str . length -1 ; J >=0 ; J –)

                                               {

                                               newstring  + = str[ J ] ;

}

// return the reverse string

return newstring ;

}

reversestring ( ‘hello ‘ ) ;

Reverse a string with recursion

► Play

To solve this solution, we will use two methods: 1st  the string. prototype. substr ( ) method and 2nd the string. prototype. charAt ( ) method.

. The substr ( ) method returns the characters in a string beginning at a specified location through the specified number of character.

     e.g.          “string” .substr ( 1 ) ;  

. The charAt method return the specified character from a string.

    e.g.          “string” .charAt ( 0 ) ;

The depth of recursion is equal to the length of the string. The solution to this type of question is not interesting and will be very slow if the string is so long. In this solution the size of stack is very important.  

Example code

Function reversestring ( str )

{

if ( str === “ ” )

Return “ “ ;

else

return reversestring ( str . substr ( 1 ) ) + str . charAt ( 0 ) ;

reversestring ( “ string “ ) ;

How to reverse word in a string in javascript

► Play

  • Split the desired string by space character delimiters
  • Determine how many words can be separated
  • Return the new string

Example code

function StringUtil ( )

{

this . reverseWords = function ( str )

{

var result = “ ” ;

var wordarray = str . split ( “ “ ) ;

for ( var i = wordarray . length -1 ; i >= 0; i– )

{

result += wordarray[i] + “ “;

}

return result.trim();

}

}

Send us a feedback0/500

Do you like this article?
Yes
No