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 tex...
For further actions, you may consider blocking this person and/or reporting abuse
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.The solution in PHP
Strings in Rust are a bit tedious since you usually want to avoid extra allocations and there's ownership concerns, so this code has a lot more
ref
s than I'd like, and the function signature is a classic, but otherwise I think it's kind of pretty.What I like most is that there's no
-2
for the length, because Rust pattern matching lets me capture the elements into another slice right there.Permalink to the Rust Playground: play.rust-lang.org/?version=stable...
Elixir
There's probably a much simpler way to solve it, but hey, I basically know how to do pattern matching and recursion, so there we go:
Here is my proposition in JS:
Wasn't this in a previous challenge? I recall building this in HTML+CSS...
I think so Alvaro, i've also done this in python...
this is the one: dev.to/thepracticaldev/daily-chall...
Javascript one-liner.
The cutoff can be changed.
This is a case where the switch block is probably the simplest idiom with the least cognitive load.
Having to use Math.min, conditional Regex's, replace callbacks, and array shifts, would be unnecessary complexity.
Good work showing the variants to outline the pros and cons though.
The exception would be if the more "complex" solution was more performant.
For verification, I ran a perf test on the two funcs, and the switch idiom won out. (see screenshot below).
If another idiom were to beat out the switch idiom, I would swap out the implementation for the fastest idiom that uses the least cycles.
Wrap it in a function, document it, and include a resource link to the performance notes, tests and reasoning of the chosen implementation.