[Day17] Sorting Band Names without articles

[Day17] Sorting Band Names without articles

文字顺序排列

需要用到的技巧与练习目标

  1. sort
  2. trim
  3. replace

const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];

const strip(bandName){
return bandName.replace(/^(a |the |an  )/i,'').trim();
}

const sortedBands = bands.sort((a,b)=> strip(a)>strip(b)?1:-1)


document.querySelector('#band').innerHTML=sortedBands.map(item =>
`<li>${item}</li>`
).join('');



<<:  Day 17 - canvas 文字换行

>>:  #18 用免费 Serverless 及 JavaScript 写 Telegram 聊天机器人!

Day2:安装Azure AD Connector同步至M365遇到TLS 1.2卡卡要怎麽办

当我们正准备将企业AD帐号透过传送门(Azure AD Connector)运送到Microsoft...

Day 18: Behavioral patterns - Chain of Responsibility

目的 当一个行为需要经过多道步骤时,可以将步骤串在一起。 说明 该模式常见於两种情境: 行为不一定一...

JS 执行环境与执行堆叠 DAY48

执行环境(Execution context) 我们上一篇有提到 若在 callMe 宣告任何变数 ...

【C language part 4】阵列与字串&函式

阵列 阵列是一群具有相同名称或资料型态的变数集合。 由於整个阵列中的变数均具有相同的名称,因此若要存...

Day3 众里寻它千百度

Filter 在海量的数据中如果想要快速找到特定关键字的资料,我们需要filter这个很实用的功能...