- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements