使用 JavaScript 对字符串进行按字母顺序排序并插入下划线


问题

我们需要编写一个 JavaScript 函数来接收一个字符串数组。

我们的函数应按照字母顺序对数组中的字符串进行排序,并且该字符串的每个字符都应由“***”隔开。

示例

以下是代码 −

 在线演示

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));

输出

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

更新于: 17-4-2021

292 浏览

开启你的职业生涯生涯

完成课程以获得认证

开始
广告
© . All rights reserved.