Open In App

Mirroring of Sentence in JavaScript

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

First, let’s understand what’s the meaning of the mirroring of a sentence with the help of an example given below: 

Assume, sentence = “GeeksforGeeks is an Excellent Platform” So, mirrored Sentence will be “skeeGrofskeeG si a tnellecxE mroftalP”.

There are three methods that will be used for the implementation of code for the mentioned Problem that are: 

  • JavaScript split() Method: This method converts the string type into the array type.
  • JavaScript reverse() Method: The reverse method reverse the elements of the array. It only works when the data structure is an array. And the output will be given an array type.
  • JavaScript join() Method: This method works by joining the elements of the array and converting the array type into string type.

Let’s understand through step-by-step implementation.

Step 1: First, we will split the given string into individual(or single) characters by using the split method so that we can mirror the words of the sentence. let’s take a look at the code of it.

Javascript




<script>
    const sen = "GeeksforGeeks is a Excellent Platform";
    // This will split the sentence into alphabet and will
    // give you array Data Structure
    const res = sen.split(''); 
    console.log(res);
</script>


Output:

Step1 Output

Step1 Output

Step 2: After splitting, we will reverse the elements of the array so that we can join the array in reverse order to obtain every word as a mirrored word.

Javascript




<script>
    const arr = [                         
          'G', 'e', 'e', 'k', 's', 'f', 'o',
          'r', 'G', 'e', 'e', 'k', 's', ' ',
          'i', 's', ' ', 'a', ' ', 'E', 'x',
          'c', 'e', 'l', 'l', 'e', 'n', 't',
          ' ', 'P', 'l', 'a', 't', 'f', 'o',
          'r', 'm'
    
    const reverseArray = arr.reverse(); 
    console.log(arr);
</script>


Output:

Step2 Output

Step2 Output

Step 3: Now we will use the join method to join the characters of the array that we had obtained from the previous step to get the mirrored word as an output in this step. 

Javascript




<script>
    const revArray = [                      
        'm', 'r', 'o', 'f', 't', 'a', 'l',
        'P', ' ', 't', 'n', 'e', 'l', 'l',
        'e', 'c', 'x', 'E', ' ', 'a', ' ',
        's', 'i', ' ', 's', 'k', 'e', 'e',
        'G', 'r', 'o', 'f', 's', 'k', 'e',
        'e', 'G'
      ]
  
    // joinArr will be of string data type
    const joinArr = revArray.join('');  
    console.log(joinArr);
</script>


Output:

Step3 Output

Step3 Output

Step 4: In the previous step we mirrored the word but the sentence has got reversed also. So now, we will again reverse the sentence to reorder it. We will use the split method again to split the sentence into words. The question that arises here is why we don’t have used the reverse method to reverse the order of the words. Because the reverse method only works on the array not on the string. 

Javascript




<script>
    const mirrorWord = "mroftalP tnellecxE a si skeeGrofskeeG";     
    const splitAsWords = mirrorWord.split(" "); 
    console.log(splitAsWords);
</script>


Output:

Step4 Output

Step4 Output

Step 5: From the previous output we have got the array of words as output. Now, we will use reverse method to reverse the order of words to get mirrored Sentence. 

Javascript




<script>
    const wordsArr = [ 'mroftalP', 'tnellecxE'
        'a', 'si', 'skeeGrofskeeG' ]; 
    const revOrder = wordsArr.reverse(); 
    console.log(revOrder);
</script>


Output:

Step5 Output

Step5 Output

Step 6: In the previous step we got our mirrored Sentence in the Array type. Now, we just have to join the words by using the join method to get the output as string type.

Javascript




<script>
    const mirroredArr =  [ 'skeeGrofskeeG', 'si', 'a',
    'tnellecxE', 'mroftalP' ]; 
    const finalans = mirroredArr.join(" "); 
    console.log(finalans);
</script>


Output:

Step6 Output

Final Output

Let’s see the code into one line for mirroring the sentence below:

Javascript




<script>
    const sen = "GeeksforGeeks is a Excellent Platform";
    const mirrSen = sen.split('').reverse().join('').split("
        ").reverse().join(" "); 
    console.log(mirrSen);
</script>


Output:

Output

Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads