nameList.js

Codewars 알고리즘 풀이


Problem

  • Given an array containing hashes of names.
    • 이름의 hash를 포함하는 배열이 주어진다.
  • 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.
    • 쉼표로 구분된 이름 list 형식의 문자열을 반환한다.
    • 마지막 두 이름은 ampersand(\&)로 구분한다.



Solution 01

function nameList(arr) {
  if (arr.length === 0) return '';
  
  let result = arr[0].name;
  
  if (arr.length === 1) {
    return result;
  }
  
  for (let i = 1; i < arr.length - 1; i++) {
    result += ', ' + arr[i].name;
  }
  
  return result + ' & ' + arr[arr.length - 1].name;
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo


Solution 02

function nameList(arr) {
  let result = '';
  
  while (arr.length) {
    result += arr.shift().name;
    
    if (arr.length === 1) {
      result += ' & ';
    }
    
    if (arr.length > 1) {
      result += ', ';
    }
  }
  return result;
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo


Solution 03

function nameList(arr) {
  return arr.reduce((total, current, index, array) => {
    if (index === 0) {
      return current.name;
    } else if (index === array.length - 1) {
      return total + ' & ' + current.name;
    } else {
      return total + ', ' + current.name;
    }
  }, '');
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo


Solution 04

function nameList(arr) {
  let maping = arr.map(i => i.name);
  let poping = maping.pop();
  
  return maping.length ? maping.join(', ') + ' & ' + poping : poping || '';
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo


Solution 05

function nameList(arr) {
  return arr.map(i => i.name).join(', ').replace(/^(.*)(, )(.*)$/, '$1 & $3');
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo


Solution 06

function nameList(arr) {
  return arr.map(i => i.name).join(', ').replace(/,(?=[^,]*$)/, ' &');
}

nameList([{ name: 'Sam' }]);                                      // Sam
nameList([{ name: 'Sam' }, { name: 'Ralph' }]);                   // Sam & Ralph
nameList([{ name: 'Sam' }, { name: 'Ralph' }, { name: 'Leo' }]);  // Sam, Ralph & Leo