r/learnjavascript • u/CommunicationOk9434 • Aug 29 '23
Does anyone understand this?
Who knows the answer to this?
21
u/jonnybebad5436 Aug 29 '23
Who is making you do this lol
7
u/CommunicationOk9434 Aug 29 '23
Lol no one. I saw it online and I wanted to see if anyone understood it.
22
4
u/jonnybebad5436 Aug 29 '23
Ok good to hear lol. Like others have said, I’d just run it thru the console 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
3
253
u/The80sDimension Aug 29 '23
Stupid shit you’d never do while actually writing code.
29
Aug 30 '23
yah sadly people on my team write sql this way just random spaghetti riddles
1
u/Reaver75x Aug 30 '23
Can you give an example?
4
Aug 31 '23
nested subqueries like 4 levels deep, CTE’s with names like “T1”, random inconsistent capitalization of statements, random spacing, random lines commented out, complex regex statements where LEFT() would have worked just fine, naming tables things like “Final_Table”… zero documentation… me asking what certain lines do and people who wrote the code not even knowing
the list goes on, I’ve seen nightmarish shit. This mostly comes from Business Analysts who lack a software engineering mindset
2
3
3
Aug 31 '23
I live by the KISS method. Just because you understand your complicated code doesn't mean the next person after you can untangle it when it breaks.
1
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.
1
2
32
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 with a = [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 get x[0] its 0, so 1 gets assigned to x[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"
4
6
1
Aug 30 '23
Don't forget you're only referencing the old variable. B is not given its own space in memory.
1
u/lordpuddingcup Sep 01 '23
Shhhh we’re not supposed to talk about our memory usage when discussing JavaScript
0
55
11
8
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
Aug 30 '23 edited Aug 30 '23
Not looking at the comments --
var result = ( a == b ) + ({} + {})
// Step 1: evaluate a==b
var a = []
var b = a
a[0] = a = [0] // -> a = [ [0] ]
b[1] = 0 // -> b == [ null, 0 ]
result = ( false ) + ({} + {})
// result == "false[objectObject][objectObject]true" as string concatenation
had 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.
1
u/CommunicationOk9434 Aug 31 '23
That's what I was thinking. I don't understand that part either. Those empty curly brackets make no sense to me.
2
u/og-at Sep 05 '23 edited Sep 05 '23
They're empty objects. Objects and parenthesis used like this in js really seems to have seemingly weird and counterintuitive side effects, but it's actually doing what's consistent in the language.
The real problem here is that nobody would use objects like this. it's s little more than a test of if you know the esoteric rules of js and most of us plebs don't.
5
4
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
4
7
1
u/SadAd4085 Aug 30 '23
why tho
1
u/CommunicationOk9434 Aug 30 '23
I think it's supposed to be a sort of coding puzzle. It's supposed to be a least a little confusing so as to test a person's ability to understand javascript.
1
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/CommunicationOk9434 Aug 31 '23
It's supposed to be a sort of puzzle to test people's understanding of javascript.
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
1
3
u/Fun_Comfort_180 Aug 30 '23
It means you should learn to avoid this, this is an example of horrible code.
3
3
3
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
3
2
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
2
2
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
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
1
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
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
1
1
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
0
0
0
142
u/akuma-i Aug 29 '23
Just put it to the console. No one would really execute this in memory