In today's challenge, you are asked to create a like system from Facebook; a text which displays the names of people who liked an item.
For examp...
For further actions, you may consider blocking this person and/or reporting abuse
CSS
This is a "don't do this at home" type of solution. Will it work? Yes. Is is worth it? We can all agree that CSS is always worth it (:P), but in this case, it may end up with a ton of unnecessary HTML code.
The idea is to use a
ul
/ol
to represent the list of people, and with counters keep track of the number of likes (aka people in the list). Then with pseudo-elements display the appropriate message or connector.And here is a demo on codepen:
Thanks to this challenge, I learnt that CSS counters don't increment in elements with
display: none
. Which is nice.Also, I'm more of an Oxford-comma type of person, but the challenge didn't have it. (That is my excuse and I'll stick to it)
Bloody brilliant!
Some simple pattern matching with Haskell.
ruby <3
Nice!
I can't wait ruby 2.7 release!
JS, with each case defined. I went for straightforward, though there's probably room to condense this somehow given the repetition:
lol great minds think alike
Using destructuring with JS
Rust Playground
And a bit of golf with the Intl.ListFormat tool!
Here is the output:
Basically, with a bit of wibbly wobbly trickery, depending on the input length, we either call the
format
function with["no one"]
, the complete input, or the two first elements followed by the remaining quantity of items. Finally, we add (or not) ans
to thelike
, and return the built string!Trying with Gwion
Thank god for template strings.
PHP
Late to the party and I can see people have already posted a similar solution but just going to post anyway. Sometimes keeping it simple is OK :)
My simple solution in js
At first i've used a lot of
if
statement then i got excited and tried another pointPython version (using as fewer
ifs
as possible):Possible improvements: instead of
You can write:
Or - if you want to get fancy:
Or even:
But of course - all of this is redundant, since you already check for
likes_quantity > 3
. So how about this:Nice, it was in fact redundant getting the message content, thanks for sharing :D
My code in JS
let peoples = [];
let people_likes = (p) => {
let tam = p.length;
if(tam === 0){
console.log("no one likes this");
}
else {
if(tam < 4){
console.log(
${p.join(", ")} likes this
);} else {
console.log(
${p.slice(0, 2).join(", ")} and ${tam - 2} others like this
);}
}
};
people_likes(peoples);
peoples.push("Peter");
people_likes(peoples);
peoples.push("Jacob");
people_likes(peoples);
peoples.push("Mark");
people_likes(peoples);
peoples.push("Alex");
people_likes(peoples);
peoples.push("Reynaldo");
people_likes(peoples);
My take at the challenge written in Elm.
Source-code
Try it online
On Ellie App.
I question the validity of this challenge. No Oxford comma??? 🧐
But on with it...
Also Python, but using a different approach:
Python solution using a dict as switch case. Sadly I didn't had a cool idea about the "default" case wiithout using a default dict (didn't wanted to import anything)
Perl solution. The special array
@_
contains the arguments of a subroutine, but in scalar context it returns the number of them.0xford comma included.
More Go with tests:
likes.go
likes_test.go
Rust:
`
Here is my simple solution with PHP:
Python goes there :
A bit of functional JS
Ooh, a ternary chain. Nice 👍
[ gist.github.com/devparkk/505d7edd1... ]