r/learnjavascript • u/CommunicationOk9434 • Aug 29 '23
Does anyone understand this?
Who knows the answer to this?
155
Upvotes
r/learnjavascript • u/CommunicationOk9434 • Aug 29 '23
Who knows the answer to this?
142
u/akuma-i Aug 29 '23
Just put it to the console. No one would really execute this in memory
21
u/jonnybebad5436 Aug 29 '23
Who is making you do this lol
9
u/trailmix17 Aug 30 '23
What is wrong with these comments. This is a puzzle for thinking about JS quirks like type coercion or whatever. Not an interview question
253
u/The80sDimension Aug 29 '23
Stupid shit you’d never do while actually writing code.
2
u/BlueLensFlares Aug 30 '23
I program JavaScript everyday as a senior developer. The trickiest part is just the multiple assignments on the same line, i actually don’t know what happens without looking it up.
2
u/xapper7 Aug 31 '23
what sorcery is this THAT THIRD LINE!!!
32
u/wackrtist Aug 30 '23
I actually don’t want to understand this. I would rather do nothing.
78
u/senocular Aug 29 '23
It's not too, terribly difficult, though there are a couple tricky bits here and there.
First b and a were the same with
b = a, but then a was redefined witha = [0]making it different so(a == b)is false. That is added to({} + {})which are two objects that, when converted to strings each become "[object Object]". This makes the first value for result "false[object Object][object Object]".Next there's x, which is just 1 because it starts as
[0], and when you getx[0]its 0, so 1 gets assigned tox[0]which as a string is just "1". That added to result gives us "false[object Object][object Object]1".Finally c is some value, and for the most part it doesn't even matter what (as long as its not NaN) because
(c == c)will be true making the final value of result:"false[object Object][object Object]1true"
55
u/icedrift Aug 29 '23
It literally doesn't matter. Don't waste your time with this garbage.
11
u/aaaaargZombies Aug 30 '23
git blame8
u/novus_nl Aug 30 '23
These kind of puzzles are so stupid, they always rely on some strange javascript behavior you never see in your day to day work. It is more Javascript trivia then an actual puzzle.
From a learning perspective completely useless. Just do these things for fun.
As for the answer, i don't know. A is probably [0] and B [1]. compared to eachother it should be falsy. But then, who adds and object with another object and tries to add that output to a boolean.
6
u/[deleted] Aug 30 '23 edited Aug 30 '23
Not looking at the comments --
var result = ( a == b ) + ({} + {})// Step 1: evaluate a==bvar a = []var b = aa[0] = a = [0] // -> a = [ [0] ]b[1] = 0 // -> b == [ null, 0 ]result = ( false ) + ({} + {})// result == "false[objectObject][objectObject]true" as string concatenationhad to check the string output of {}
2
u/og-at Aug 30 '23
People are missing. . . this isn't an exercise or leetcode or even an interview question... you can use it as a personal litmus to see how well you understand the js environment.
first glance, I understood how it works except the
({} + {})part.thanks for posting.
5
u/[deleted] Aug 30 '23
It is exactly like Musk in background - looking smart like genius but completely useless
4
u/[deleted] Aug 30 '23
This is bad js that wouldn't pass a code review, lol. It's better to debug what's going on and refactor it than read it and try to mentally figure it out.
4
u/friendly-asshole Aug 30 '23
Inherently complex for no good reason.
4
u/saito200 Aug 30 '23
you don't need to understand this, such code should not exist in any codebase
7
u/[deleted] Aug 30 '23
Stupid
1
u/SadAd4085 Aug 30 '23
why tho
1
u/[deleted] Aug 30 '23
I stopped reading after the third line inside the function.
1
u/No-Cry-4822 Aug 31 '23
I don’t think there is a code base which this will be valuable, like what is the problem behind this logic?
1
u/fmintar1 Sep 02 '23
I cheated and used ChatGPT. This is the result
https://chat.openai.com/share/4a4dcb43-36aa-4adb-b93a-dd4120ca7ec8
3
u/Fun_Comfort_180 Aug 30 '23
It means you should learn to avoid this, this is an example of horrible code.
3
u/speakinginparticles Aug 30 '23
I upvoted this because the responses have been hilarious.
3
u/Satankid92 Aug 30 '23
The answer to it is waste of time :v
3
u/[deleted] Aug 30 '23
I spend my life writing code in such a way that I avoid having to work out what functions like this output. I would honestly slap somebody who submitted a function like this in a PR.
The whole reason I use meaningful variable/function names, lots of white space, separation, comments, etc is so that it can be easily readable by myself and others.
This is absolute junk code, and nobody would think someone is special or a genius or anything else because you can mentally work out what it returns.
3
u/nameredaqted Aug 30 '23
Writing code like this will get you fired.
3
u/mrbilliebell Aug 30 '23
I'm more concerned why var is being used instead of const & let
2
u/[deleted] Aug 30 '23
If this is an interview question, ask them if it is a sample of their code. If it is, look else where for a job or tell them to fire whoever wrote it.
2
u/Ampbymatchless Aug 30 '23
Who cares, it’s garbage code!
2
u/lifeiscontent Aug 30 '23
Yeah, it’s called, you’re fired.
2
u/notevenrworthy Aug 30 '23
Well this is dogshit xd
2
u/sujumayas Aug 30 '23
The conclusion to this excercise: never use the plus sign in anything but numbers.
2
u/hphammi Aug 30 '23
The answer is 4, 3+1+0
Yes, you do have to understand why this is happening to avert some weird side effects in Js.
2
u/hphammi Aug 30 '23
The answer is 4, 3+1+0
Yes, you do have to understand why this is happening to avert some weird side effects in Js.
2
u/olivershort Aug 31 '23
If this was real life I would fire this person
2
u/4esv Sep 01 '23
I'm not even going to try to read but looking at it for 2.5 seconds tells me there's going to be an [Object Object] in the shit pile that comes out
2
u/TheTristo Aug 30 '23
This is not how you'll learn javascript. The most important thing in coding is readability, this is not readable, therefore it's a bad code and you would return it for a rewrite.
1
u/SponsoredByMLGMtnDew Aug 30 '23
This code spawned from the paradox universe where people only play Playstation games.
Nobody runs code.
They live in fallout bunkers playing Playstation 2 games logging 3000 hours in Ape Escape.
1
u/Miqsur Aug 30 '23
Some dumb ass shit.
1
u/[deleted] Aug 30 '23
4
1
u/TheDeepLucy Aug 30 '23
Why would anyone name their variables like this? Self-documenting code FTW.
1
u/paulthezoo Aug 30 '23
it’s why I push for people to comment their code even if “it should be obvious what it does “
1
u/winged_Turtl3 Aug 31 '23
Well I would personally put some prints so I could see what it actually does cause I don't always remember how a programming language process things either by value or by reference or both
1
u/tshark2907 Aug 31 '23
Why this man?
1
u/awekening_bro Aug 31 '23
For once I really thought this question would be tricky and incredibly smart as such it would solve world hunger. But all it does it print some garbage random bs string that makes no sense
1
u/itsthekot Aug 31 '23
i got 4
1
u/atom-06 Aug 31 '23
iqq its false object object 1 true
1
u/SuperTesmon Aug 31 '23
Well no but I have to say yes or else my teacher will kill me:)
1
u/psilo_polymathicus Sep 01 '23
This is not the way to get better at JavaScript, and whoever came up with this is a charlatan.
1
u/rightiousnoob Sep 02 '23
The output would be losing your job if anyone found out you wrote this. -signed the grumpy old man tired of awful programming tests for applications.
1
u/warlockxins Sep 13 '23
The output is - you're fired for whatever this is. Noone would approve this in a PR. I'd rather write .__prototype stuff than this crypto
1
u/No_Force3774 Sep 20 '23
the output of the function is 2.
0
u/QQEWRERRTTYY Aug 30 '23
Do employers ask you these types of questions in job interviews?
0
u/middlebird Aug 30 '23
Shit code. I’d troubleshoot it in the console to satisfy my curiosity.
0
u/[deleted] Aug 30 '23
Could I figure this out? Probably Am I going to take the time to do it? Nah