Skip to content

Instantly share code, notes, and snippets.

@bondarewicz
Last active January 22, 2021 11:54
Show Gist options
  • Save bondarewicz/8841713 to your computer and use it in GitHub Desktop.
Save bondarewicz/8841713 to your computer and use it in GitHub Desktop.
JS: Simple Like function
function likes(names) {
names[0] = names[0] || "no one";
if (names.length > 3) names[2] = names.length-2 + " others";
return names.slice(0,3).join(", ").replace(/(.*), /, "$1 and ") + " like" + (names.length<2 ? "s" : "") + " this";
}
alert(likes([])); // must return "no one likes this"
alert(likes(['Peter'])); // must return "Peter likes this"
alert(likes(['Jacob', 'Alex'])); // must return "Jacob and Alex like this"
alert(likes(['Max', 'John', 'Mark'])); // must return "Max, John and Mark like this"
alert(likes(['Alex', 'Jacob', 'Mark', 'Max'])); // must return "Alex, Jacob and 2 others like this"
alert(likes(['Alex', 'Jacob', 'Mark', 'Max','Luki'])); // must return "Alex, Jacob and 3 others like this"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment