使用 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.