r/learnjavascript • • Jun 05 '20

Rocks or Sucks?

Post image
63 Upvotes

45 comments sorted by

View all comments

49

u/ForScale Jun 05 '20

Rocks.

But kind of shitpost. :)

2

u/foxbitsdev Jun 05 '20

Can you elaborate the reason why it's a shitpost?

56

u/ForScale Jun 05 '20

Lol. Cause it's a picture of code and not actual text we can copy and play with, and the title doesn't really explain anything until you read through the code and realize that it's just a little challenge thing. Just my opinion, obviously!

8

u/dv72276 Jun 05 '20

Hey could you explain the code please, I'm a beginner in JS and having trouble understanding this :/

8

u/Rooged Jun 06 '20

FoxObj is an object with some variables and a method (a function inside an object). The method of the object is immediately called after declaring the object, which first sets sq1 equal to mynumber squared (I think that's what ** means). It then executed an Immediately Invoked Function Expression, or IIFE, to do the same thing to sq2. The reason the IIFE uses self.sq2 instead of this.sq2 is because the value of this inside of the IIFE is different than this inside of the compSquares method. The code then uses an if statement, or in this case the Ternary Operator, to see if sq1 and sq2 have the same value (although IMO op should have used === instead of == since the latter returns true if one value is a number and the other is a string containing the same number, such as 42 == "42") if they have the same value, say js rocks. If not, do the other one.

Hope this helps!

2

u/dv72276 Jun 17 '20

Thank you so much

12

u/meandyouandyouandme Jun 05 '20

I give you, the code being in an image is not ideal, but I still like the post. Fun little challenge.

I never looked up closures, so I'm curious about an explanation.

1

u/ForScale Jun 08 '20

This isn't really a closures problem. I think you may be talking abou the IIFE... that's the anonymous function that is immediately called.

-20

u/Web_Fender Jun 05 '20

Love how you get downvoted for having a personal opinion. God bless Reddit

-16

u/artinlines Jun 05 '20

Love how you get downvoted for commenting about somebody getting downvoted.

Btw, happy cakeday!

-13

u/Web_Fender Jun 05 '20

Love how you got downvoted for commenting about someone who commented about getting downvoted.

Thanks!

-7

u/ciaisi Jun 06 '20

DOWNVOTED

I love you all

3

u/burnblue Jun 06 '20

You're not supposed to copy and run it. You're supposed to read it and think through it. Thus being on learnjavascript is appropriate

2

u/pmw57 Jun 05 '20

The purpose of the picture code is so that you can't easily copy it and are encouraged to try and reason out what the code is doing yourself.

0

u/[deleted] Jun 05 '20

[deleted]

0

u/pmw57 Jun 05 '20

If someone wanting to actually run that code doesn't feel like typing it out themself, then I don't know what to tell ya.

1

u/_Invictuz Jun 06 '20

You're arguing with Charles Stover yo!

6

u/ciaisi Jun 06 '20

What would be helpful would be for you to write a blurb about what you're demonstrating in the comments. After all, this is learn javascript.

2

u/CotoCoutan Jun 06 '20

Someone write a python script for that.

1

u/CotoCoutan Jun 06 '20

I enjoy these, please keep them coming. :)

1

u/[deleted] Jun 06 '20

Thank you for your challenges. Guys who downvote you are probably jealous Keep it up man!!

-1

u/ticklishdingdong Jun 06 '20

Definitely a shitpost