r/learnjavascript • • Aug 29 '23

Does anyone understand this?

Post image

Who knows the answer to this?

155 Upvotes

105 comments sorted by

142

u/akuma-i Aug 29 '23

Just put it to the console. No one would really execute this in memory

26

u/CommunicationOk9434 Aug 29 '23

Good idea. It's supposed to be some sort of coding exercise so I wanted to see if anyone could understand it without console.

70

u/akuma-i Aug 29 '23

Well, if you do some obfuscation in JS, you might be able to solve this. If you’re normal - you have never used such things in your code, so you know it works and don’t know how.

I have 15+ years of experience and don’t know what the third line does :)

11

u/toroga Aug 30 '23

Oh thank goodness, because that’s where I stopped too lol

1

u/CommunicationOk9434 Sep 02 '23

15 years and you don't fully understand it? I've been a coursera student since February , so I definitely don't.

2

u/akuma-i Sep 02 '23

Why would I? Why should I? I don’t write such type of code at all. “Understand” code means you not only know what is happening, but you also know why this is happening. So here, of course, I know what it dies, but “why”? I don’t even want to know, it’s a piece of useless garbage.

1

u/chrizerk Aug 31 '23

Line 3? First it's replacing what a is pointing to. Its now pointing to a new array of length 1 that has the value 0 at index 0, then replacing the value at index 0 with a reference to itself....I think lol. But why you would ever do that is beyond me. Remember kids, don't drink and code.

1

u/No-Ordinary-Prime Sep 01 '23

third line appears to create [ [ 0 ] ]

0

u/[deleted] Aug 30 '23

[deleted]

3

u/og-at Aug 30 '23

You sir, apparently ask too much.

Just remember. . . this is your employment competition.

4

u/maubg Aug 29 '23

You, take a paper, pencil and ruler. We are making a symbol table and array representations.

5

u/[deleted] Aug 30 '23

The point is to grok the code I assume.

If you can grok this Jackson Pollackian monstrosity, you can grok well.

3

u/[deleted] Aug 29 '23

God I though Im dumb. I have been learning coding for about a year and I fucking dont exert effort on memorizing javascript rules. I know there's scoping, async, queue stack behind the hood, but I cant tell you off the bat exactly what they are. I just know scope means there's instances variable is not accessible, async waits for something to execute, and queue stack is the order of execution.

I had read and understand them once but I dont remember everything bout them

10

u/sivadneb Aug 30 '23

Coding is not really about memorization. It's about learning by doing. Just keep building stuff. Eventually things will start to click.

2

u/og-at Aug 30 '23

Every programming language is toolbox used for solving problems.

JS has a pretty hefty toolbox to solve problems, just like Python, just like C#. Being a dev/programmer is about figuring out how to tell the computer to solve a problem using the given tools.

Remembering the exact syntax and use case of the different array methods is the same as looking at a socket set and knowing their exact sizes just by looking. On the other hand, knowing how array.map works is like using a 10mm socket... you use it a lot so it's easy to recognize.

For all the other sockets, there's the printing on the side, or trying it on the nut . . . the equivalent of using the documentation.

Don't memorize. Just use it and you'll remember. Do enough projects, you'll remember enough tools, and you'll be able to solve more complex problems off the top of your head.

And this is why everyone will tell you to build build build. Just ask Alan Iverson.

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

u/digga-wat Aug 30 '23

if you're in danger, blink twice.

5

u/CheeseFest Aug 30 '23

I saw it online

Honestly I think OP should turn this job offer down!

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

253

u/The80sDimension Aug 29 '23

Stupid shit you’d never do while actually writing code.

29

u/[deleted] 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

u/[deleted] 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

3

u/Smiith73 Aug 31 '23

When the SME on your new project "Knows enough SQL to be dangerous"...

3

u/[deleted] 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

u/[deleted] Aug 31 '23

It's not just stupid, it's anti-intelligent.

1

u/fmintar1 Sep 02 '23

I'm gonna steal this quote!

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!!!

1

u/CommunicationOk9434 Aug 31 '23

LOL I don't know.

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 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

u/og-at Aug 30 '23

oh... it's you.

well done, as usual.

6

u/agapukoIurumudur Aug 30 '23

Finally someone actually answering the question, thank you !!

1

u/[deleted] 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

u/[deleted] Aug 30 '23

[deleted]

4

u/[deleted] Aug 30 '23

Welcome to JavaScript!

1

u/_Blumiere Sep 02 '23

Welcome to JavaScript done wrong!

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 blame

4

u/Wiikend Aug 30 '23

underrated comment

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

u/[deleted] 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

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/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

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/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

u/CommunicationOk9434 Sep 02 '23

And that is why a.i. will replace most of us one day.

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.

1

u/LouBagel Aug 31 '23

Or give you amazing job security

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/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?

4

u/SmellsLikeTeenSweat Aug 30 '23

Yes. They usually do this after asking you to centre a div.

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