Last active
January 22, 2021 11:54
-
-
Save bondarewicz/8841713 to your computer and use it in GitHub Desktop.
JS: Simple Like function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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