Format a string of names like 'Bart, Lisa & Maggie'.

链接: https://www.codewars.com/kata/format-a-string-of-names-like-bart-lisa-and-maggie

题目介绍

Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

list([ {name: 'Bart'}, {name: 'Lisa'}, {name: 'Maggie'} ])
// returns 'Bart, Lisa & Maggie'

list([ {name: 'Bart'}, {name: 'Lisa'} ])
// returns 'Bart & Lisa'

list([ {name: 'Bart'} ])
// returns 'Bart'

list([])
// returns ''

Note: all the hashes are pre-validated and will only contain A-Z, a-z, '-' and '.'.

解题思路

我看到题目第一反应用正则。

  • 第一步 names.map(x=>x.name),遍历得到数组: ['Bart', 'Lisa', 'Maggie']
  • 逗号分隔 .join(', ') 得到字符串:'Bart, Lisa, Maggie'
  • 正则替换最后一个 ', '' & '

参考答案

一句话代码解决方案,我的提交:

const list = names =>
  names.map(x=>x.name)
    .join(', ')
    .replace(/,\s(\w+)$/,' & $1');

拓展阅读

《边学边玩酷JS》

© Willin Wang            更新时间 2021-02-23 16:29:35

results matching ""

    No results matching ""