Transforming array of numbers to array of alphabets using JavaScript

Problem

We need to write a JavaScript function that takes an array of numbers and transforms it into a string with four parts separated by hyphens. Each part creates a 4-character "word" from the first two and last two elements of the array under different conditions.

The four parts are:

  • Characters from the original array (first, second, second-last, last)

  • Same as above, after sorting the array in ascending order

  • Same as above, after sorting the array in descending order

  • Same as above, after converting to ASCII characters and sorting alphabetically

Example Implementation

Here's how to solve this step by step:

const arr = [99, 98, 97, 96, 81, 82, 82];

const transform = (arr = []) => {
    // Part 1: Original array - first, second, second-last, last
    let part1 = [];
    part1.push(arr[0], arr[1], arr[arr.length-2], arr[arr.length-1]);
    part1 = part1.map(x => String.fromCharCode(x)).join('');
    
    // Part 2: Ascending order
    const ascending = [...arr].sort((a, b) => a - b);
    let part2 = [];
    part2.push(ascending[0], ascending[1], ascending[ascending.length-2], ascending[ascending.length-1]);
    part2 = part2.map(x => String.fromCharCode(x)).join('');
    
    // Part 3: Descending order  
    const descending = [...arr].sort((a, b) => b - a);
    let part3 = [];
    part3.push(descending[0], descending[1], descending[descending.length-2], descending[descending.length-1]);
    part3 = part3.map(x => String.fromCharCode(x)).join('');
    
    // Part 4: ASCII characters sorted alphabetically
    const asciiSorted = arr.map(el => String.fromCharCode(el)).sort();
    let part4 = asciiSorted[0] + asciiSorted[1] + asciiSorted[asciiSorted.length-2] + asciiSorted[asciiSorted.length-1];
    
    return `${part1}-${part2}-${part3}-${part4}`;
};

console.log(transform(arr));
cbRR-QRcb-cbRQ-QRcb

How It Works

The function processes the array in four different ways:

  • Original order: Takes positions [0, 1, length-2, length-1] and converts to ASCII characters

  • Ascending sort: Sorts numbers numerically, then takes the same positions

  • Descending sort: Sorts numbers in reverse order, then takes the same positions

  • Alphabetical sort: Converts to ASCII first, then sorts alphabetically

Compact Version

Here's a more concise implementation:

const transformCompact = (arr = []) => {
    const getCorners = (array) => {
        return array[0] + array[1] + array[array.length-2] + array[array.length-1];
    };
    
    const toChars = (nums) => nums.map(x => String.fromCharCode(x));
    
    const part1 = getCorners(toChars(arr));
    const part2 = getCorners(toChars([...arr].sort((a,b) => a-b)));
    const part3 = getCorners(toChars([...arr].sort((a,b) => b-a)));
    const part4 = getCorners(toChars(arr).sort());
    
    return `${part1}-${part2}-${part3}-${part4}`;
};

console.log(transformCompact([99, 98, 97, 96, 81, 82, 82]));
cbRR-QRcb-cbRQ-QRcb

Conclusion

This function demonstrates array manipulation, sorting techniques, and ASCII character conversion. The key is understanding how different sorting methods affect the final character positions and outputs.

Updated on: 2026-03-15T23:19:00+05:30

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements