Mengembalikan rangkaian nama, dipisahkan oleh koma dan ampersand namesoBj di javascript

function list(names){
  let answer = [];
  let result;
  
  if (names.length === 0) return '';

  for (let n in names) {
    const values = Object.values(names[n]).join('')

    if (names.length === 1) {
      return values
    } else if (names.length === 2 ) {
      answer.push(values)
      result = answer.join(' & ')
    } else {
      answer.push(values);
      result = answer.join(', ')
      let n = result.lastIndexOf(', ')
      result = result.slice(0, n) + result.slice(n).replace(', ', ' & ');
    }
  }
 
  return result
}
Clever Chamois