How to use Array.map() Method In Javascript

In this method, we convert the string into an array of characters using split(”). Then, we use the map() method to iterate through each character. For characters after the specified starting position, we calculate their mirrored counterparts based on their ASCII values. Finally, we join the characters back into a string.

Example:

JavaScript
function mirrorStringFromPosition(inputString, startPosition) {
    const startIdx = startPosition - 1;
    const mirroredString = inputString
        .split('')
        .map((char, index) => {
            if (index >= startIdx) {
                // Calculate mirrored character based on ASCII values
                const mirroredCharCode = 219 - char.charCodeAt();
                return String.fromCharCode(mirroredCharCode);
            }
            return char;
        })
        .join('');

    return mirroredString;
}

const givenString = "w3wiki";
const startingPosition = 5;
console.log(mirrorStringFromPosition(givenString, startingPosition));

Output
geekhulitvvph

Time Complexity: O(n)

Space Complexity: O(n)




JavaScript Program to Mirror Characters of a String

Our task is to mirror characters from the N-th position up to the end of a given string, where ‘a’ will be converted into ‘z’, ‘b’ into ‘y’, and so on. This JavaScript problem requires mirroring the characters in a string starting from a specified position. There are various approaches available to accomplish this, such as utilizing loops and maps. In this article, we will explore various methods to find the mirror characters of a string.

Examples:

Input : N = 3
paradox
Output : paizwlc
We mirror characters from position 3 to end.
Input : N = 6
pneumonia
Output : pnefnlmrz

Table of Content

  • Method 1: Creating a String:
  • Using a for loop
  • Using Custom Mapping
  • Using Array.reduce() Method
  • Using Array.map() Method

Similar Reads

Method 1: Creating a String:

In this method, we maintain a string (or a character array) containing the English lowercase alphabet. Starting from the pivot point up to the length of the string, we retrieve the reversed alphabetical counterpart of a character by using its ASCII value as an index. By applying this approach, we convert the given string into the desired mirrored form....

Using a for loop

Initialize an empty string to store the mirrored result.Iterate through the characters of the given string using a loop.When the loop index is equal to or greater than the starting position, replace the current character with its mirror (e.g., ‘a’ becomes ‘z’ and ‘b’ becomes ‘y’).Append the mirrored character to the result string....

Using Custom Mapping

Create a character mapping to represent the mirror transformations.Initialize an empty string for the mirrored result.Iterate through the characters of the given string.If the character is in the mapping, replace it with the mapped character; otherwise, keep it as is....

Using Array.reduce() Method

Using the Array.reduce() method to mirror a string involves converting the string into an array of characters, then applying reduce to accumulate the characters in reverse order, effectively reversing the string....

Method 5: Using Array.map() Method

In this method, we convert the string into an array of characters using split(”). Then, we use the map() method to iterate through each character. For characters after the specified starting position, we calculate their mirrored counterparts based on their ASCII values. Finally, we join the characters back into a string....