You probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.
Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in these examples:
likes {} // must be "no one likes this"
likes {"Peter"} // must be "Peter likes this"
likes {"Jacob", "Alex"} // must be "Jacob and Alex like this"
likes {"Max", "John", "Mark"} // must be "Max, John and Mark like this"
likes {"Alex", "Jacob", "Mark", "Max"} // must be "Alex, Jacob and 2 others like this"
For 4 or more names, the number in and 2 others simply increases.
Good luck!
This challenge comes from BattleRattle on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (24)
This'd be a good use for template literals. Here's a quicky in javascript:
Sanity check:
Here is the code snippets with PHP:
I think you don't need the break after the return, it would be unreachable.
TypeScript
C# switch expressions to the rescue!
For Python
Test it:
Here's my version :)
Very declarative Haskell solution:
Ruby string-formatting takes an array of arguments, so if my function takes an array of names, then I just need to feed it into some pre-formatted "templates". Also the use of splat-array which I don't completely understand actually.
Cool I did this one 3 years ago over at CodeWars, here is the answer I posted:
Looking at it now,
break
is not needed, as return breaks out of the switch statement.Some comments may only be visible to logged-in visitors. Sign in to view all comments.