Sorting string alphabetically and inserting underscores using JavaScript

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 17-Apr-2021 13:12:12
Advertisements