Hello Folks 👋
This is Savio here. I'm young dev with an intention to enhance as a successful web developer. I love building web apps wit...
For further actions, you may consider blocking this person and/or reporting abuse
😊
But who has the need in the real world / real apps to reverse a string? Nobody.
Faster !== Correct ... his string reverse breaks with code points, while
[...str]
doesn't.Try it: OP reverse breaks the emoji with
reverse('some 💩');
while this reduce suggestion doesn't.P.S.
In a bid to be obnoxious 😏 you could have shortened your code further by 2 characters 🌚
I made no claim to it being faster, but in this case the
reduce
method is almost twice as fast - jsbench.me/3tkttupth4/1 - at least on Chrome. On Firefox though, the situation is reversed. Different JS engines, different optimisations I guessalternative reverse:
:D
Nice list!
Some things I want to mention
const
(orlet
) in the first code blockObject.fromEntries(new URLSearchParams(window.location.search))
For 2, I think converting to an object is pointless, nothing wrong with the URLSearchParams object (which is basically a Map)
But it's not an actual object (like
arguments
is not an actual array), so If you want to do some advanced object stuff you need to convert it to an object.What do you mean with "I have proved my superiority in frontend technologies.". Be humble, you're not superior than anyone.
He has proved that he can copy paste from StackOverflow :-)
but to distinguish the better code to copy is the hardest thing
Yes, especially when one post has 1k votes and another has -16?
I believe English is not his primary language(not mine either). I guess he means skill/competency.
Dont be hard on him he's just a kid (he's 14)
He's 14
Nah, these are not killer one-liners, just a beginner level. Besides that, writing something in 1 line doesn't mean good. It can sometimes harm readability, for example the first snippet is just a good example of how NOT to write your code.
And some of the examples are pretty useless. Why would you create a function for
window.scrollTo(0, 0)
? It's short enough on its own to use it directly.In fact you just stolen all these snippets either from 30secondsofcode.org/ or from StackOverflow (I checked) and you have audacity to call yourself "superior". If you are really "an enthusiastic frontend developer" as you call yourself, than go and learn instead of posting useless articles.
He's 14 though, give him a break.
hahah seriously?
go do a background research on him, 14 and oversmart and fooling on internet.
Once I used to use JS to capitalize, and everything else.
Feedback comments came in from Experts
Now I just use the simpler CSS.
Ex: w3schools.com/cssref/pr_text_text-...
That doesn't do the same thing, merely changes the appearance of the string on screen - doesn't change the string. Depending on the use case, that could be okay
Yes, but that was an example (Ex:) for conversation.
I used to do everything in JS
including building accessible menus, etc
wrote several 100s of lines of code for each.
In CSS, it was always less than 50 each, very easy to change, and by everyone.
I think there is also an issue with your
randomHex
function - it will never return#ffffff
since:can only ever return values from
0
tox-1
. You need to useMath.round()
Suggested alternative:
const capitalize = ([init, ...rest]) => init.toUpperCase() + rest.join('')
Shorter:
Array shuffling this way won't be statistically random, it's biased due to how
sort
works with non-deterministic compare function. I propose changing the snippet to the similar...which assigns a random "position" to each element, compares by those, then discards them.
There are even better ways (
O(n)
).For more on this, see stackoverflow.com/q/2450954/8376184
This has come up before!
Nice post. Be aware that shuffling an Array like this is not perfectly random and a proper implementation is a little more complicated: medium.com/@nitinpatel_20236/how-t...
Since line breaks are optional in JavaScript, any code can be one line if you want to. 🤣
Ok, jokes aside, I understand one liner functions have appeal with developers, since Functional Programming is receiving full love nowadays.
So if anyone here needs to convert browser's rgb string to hex: stackoverflow.com/a/3627747/424498. This way you can directly send the result of a
getComputedStyle(someElement).backgroundColor
call to the one line function and receive the hex string as result.That's great. Just one correction: "capitalise"¹ is applied to each word in a string. In your example, it should be "upper case first" or something like that.
Best regards.
¹ developer.apple.com/documentation/...
Check if an array is not empty?
let arr = [];
arr?.some(x => x)
I'd do padStart instead of padEnd bacause of the obvious reasons.
const randomHex = () =>
#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, "0")}
;I like this one:
await new Promise(r => setTimeout(r, 2000))
wow :-)
Nice content, Savio Martin, thanks for sharing it with us!
Nice one! I wrote a similar article not long ago. Please check my take on the subject here
great
Thanks for sharing. Well done.
Good post. Just wanted to comment that making a set removes duplicates but also converts the array into a set. It stops being an array.
It's not a set because he's spreading it inside an array, so it becomes an array again.
Removing duplicates example will not work for deep objects.