Sorting string alphabetically and inserting underscores using JavaScript


Problem

We are required to write a JavaScript function that takes in an array of strings.

Our function should return the first string of the array after sorting it alphabetically and each character of that string should be separated by ‘***’.

Example

Following is the code −

 Live Demo

const arr = ['this', 'is', 'some', 'string', 'array'];
const specialSort = (arr = '') => {
   const copy = arr.slice();
   copy.sort();
   const el = copy[0];
   const res = el
   .split('')
   .join('***');
   return res;
};
console.log(specialSort(arr));

Output

a***r***r***a***y

Updated on: 17-Apr-2021

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements