Computer >> Computer tutorials >  >> Programming >> Javascript

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 −

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