DEV Community

Discussion on: Daily Challenge #266 - Who Likes It?

Collapse
dimitrilahaye profile image
Dimitri Lahaye

Here is my proposition in JS:

function whoLike(names) {
  const l = names.length;
  if (!l) {
    return 'No one likes this';
  } else if (l === 1) {
    return `${names[0]} likes this.`;
  } else if (l < 4) {
    return `${names.slice(0, l - 1).join(', ')} and ${names[l - 1]} like this.`;
  }
  return  `${names.slice(0, 2).join(', ')} and ${l - 2} others like this.`;
}