Dust off your ternary expressions, we're going in.
One-liners are tricky to maintain (and sometimes even hard to understand) but that doesn't stop...
For further actions, you may consider blocking this person and/or reporting abuse
I've got another good one to create arrays of a specific size and map over it at the same time.
Oh SHID.
This is exactly what I needed so many times, and it finally seems like it's a performant approach?!
It looks quite clean and sleek, so I decided to test out the performance on my pc
Weirdly using Array(N) is about 20% faster than using an array-like {length: N} when used inside Array.from()
I also added comparisons of mapping as a parameter vs (dot)map too.
Wow, that's... humbling.
BTW, you could beat out
ForLoop
with:since the size is known in advance.
You can shave off one more char for your hex color code generator without resorting to padEnd:
A clever change. Probably faster too?
Thanks. Not sure if it's faster, but that's usually a non-issue for code golfing :)
I was curious and created a test: jsperf.com/hex-color-code-generator
:)
Awesome! Close to what I expected 👍
I'm not a big Javascript person so I don't know what the difference is between
window.location
,document.location
and justlocation
(which I assume uses the global window context). I knowsubstr
works fine on an empty string, and I'm making the (heroically cavalier) assumption that query strings start with a?
in all browsers' implementations oflocation.search
.But both of these return something incorrect if there's no query string:
Oops. Well, we can do something about that:
And now it's longer and even less maintainable, but hey :)
This is brilliant, Ben! 😄
I can probably do it in even less characters... let me try:
Hey, that one could not accept empty value (eg:
'?a=&b=1&c='
)try this instead:
Be careful with that shuffle, it won’t yield truly random results because
sort
expects its comparator to upholdMath.sign(compare(a, b)) === -Math.sign(compare(b, a))
. Microsoft once screwed up by using exactly that.What an interesting story! Thanks for sharing.
And yes, for any production code just use Fisher/Yates!
Awesome one liners... Until you hit save and Prettier formats it to two 🤣
Why waste all those extra characters, this also works
Nice find, thanks! Edited 👍.
Does this count? 😈
I'll allow it! 😀
It's a shorthand for returning one of three colors if the score is truthy. Otherwise it returns a fallback value.
that can be even more minified:
you could lose the first array item and use score - 1, but that would have more characters.
Would it? Wouldn't you shorten by 4 chars to remove
"",
, or 3 if whitespace doesn't count, and only add 2 chars to go from[score]
to[score-1]
? Seems like it'd still be shorter by at least a character, or am I missing something?The one I recently discovered as very useful, mostly while testing or mocking API calls:
Usually I use, when I have to simulate data fetching ;P
Very cool! Thanks for sharing Dominik 👍
Thanks! I would recommend adding most of them to vscode snippets. It's really easy to use just like typing "sleep" + enter and I have ready one-liner in my code ;)
There are a lot of byte saving tips one can employ in code golfing. A nice collection can be found here: github.com/jed/140bytes/wiki/Byte-...
I did this one for a challenge. It finds if a number is in a range of two other numbers so find if b is in range of a to c
Wouldn't you just check
b >= a && b <= c
?That was definitely a way to do it. However, I am pretty sure that only works with constraints on the range values, and the way I wanted to solve it was to be sure it could handle any range.
I could be wrong which in that case, I will certainly remember this way lol
Nope to me lol, you're just right and I am pretty sure that works for any size range not just that but the
Big O is significantly better ahahaha!
I use Set often to remove duplicates, but I didn't know it's possible to use the spread operator on a Set.
I have been using
Array.from(new Set(myArray))
until now. SinceArray.from
take an iterable I could have guessed but...Nice post !
Glad it helped ya!
One of the intriguing trait of functional programming is to look unreadable despite being working.
got a problem like this:
lost key 'a' and 'c'
solution: replace the 2nd '+' by '*'
Thanks! Fixed it :)
Please don't use code in production that multiplies 86400000 (24*60*60*1000) for computing dates; date/time is way more complicated than that, you will shoot yourself in the foot. Use a library.
Not really a one-liner, but good for when making generative art.
//only rounds up 15 can be changed to what ever number you want to use
roundMinutes = (mins) => { return (~(mins / 15) * 15) * -1 ; }
roundMinutes(35) -> 45
// will now round down instead of up
roundMinutes = (mins) => { return (~~(mins / 5) * 5) ; }
great stuff
thanks
Love it!
Good stuff thanks!!
Note that list shuffle approach has a bunch of bias and doesn't perform very good shuffling. See beesbuzz.biz/code/6719-How-not-to-... for a detailed explanation.
Yup! 😀 The function is short but scary. Raphael linked another article that covers the same topic. I'll add a note.
google search got me here. Thank you for this article. This one's i've been looking for.
A couple of comments!
Anything that has semicolons in it isn't a one-liner, no matter how you try to stretch the definition.
And does it matter that days aren't always 86400000 milliseconds long?
This article makes JavaScript look really fun to play with. Thanks Andrew!
Yeah, this one was fun to write! 😁
Now I need the bash (or if Apple forces me zsh) script so I can call the command sevenDays ;)
I like it a lot.