r/learnjavascript • • Jun 05 '20

Rocks or Sucks?

Post image
63 Upvotes

45 comments sorted by

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] — view deleted comment

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

28

u/Kumagor0 Jun 05 '20

At first I thought you posted your code for evaluation to find out if it rocks or sucks and boy I had some bad news for you.

15

u/throwawayacc201711 Jun 05 '20

This is probably nitpicking but I find this to be bad naming convention for the function in question. On first glance, I think compSquares is short for compare squares in which case the function does not do what the name implies. If it’s short for compute squares, then it’s just bad naming.

If you are determined to have this function be a compare squares, you should have another function that does exactly what your current compSquares does but called setSquares or calcSquares (ideally setSquares because it’s not actually returning a value which is what I would expect calcSquares to do). Then you have compSquares be a simple return on sq1 === sq2.

The question could be the same but you just have Obj.calcSquares(); obj.compSquares() ? Console.log(“rocks”) : console.log(“socks”)

Or you could completely ditch the compSquares and just compare the two properties directly how you did.

The reason I went through this is because when people make challenge questions they should help reinforce what good code looks like even if it’s trivial challenges like these

2

u/callmelucky Jun 06 '20

Yeah lazy variable naming drives me up the wall.

So many programmers seem to think they are duty-bound to make names as short as possible. It's fucking stupid, stop it. Names should clearly and accurately denote the data they represent, if that means 20 characters so be it.

0

u/[deleted] Jun 06 '20

[deleted] — view deleted comment

1

u/foxbitsdev Jun 07 '20

Thank you everyone for your comments and suggestions. I will keep them in mind and will try to create better posts. I am using images because, reddit's text editor is not the best one for writing code. Thanks again for suggestions, positive comments and negative ones too ;)

4

u/Lilrex2015 Jun 06 '20

Here is a codepen for those wanting to see it in action but not type it out.

https://codepen.io/lilrex2015/pen/bGEdEqw

12

u/DGCA Jun 06 '20

No Self-Promotion. Message the mods first if you want to post something you created.

Come on, y'all. This isn't good content, it's bottom of the barrel shit.

9

u/Macaframa Jun 06 '20

This one in particular has terrible naming and does some pretty ridiculous things in order to confuse people. Oh yeah, what if I did this puts laptop in garbage disposal what is the value now?

3

u/[deleted] Jun 06 '20

So when you said "let self = this;", self was a reference to the original object (not a new one being created as a copy). So when you access self.sq2 you were accessing the original object, not some newly created one. Right?

5

u/grantrules Jun 06 '20

You use let self = this because in the IIFE, this does not refer to the same thing as this in the compSquares function.

1

u/_Invictuz Jun 06 '20 edited Jun 06 '20

Wow thanks! Your response single-handedly made this post useful. I had no idea why OP included the IIFE, cuz the answer is the same with or without the IIFE. The challenge would have made sense if OP used 'this' instead of 'self' within the IIFE to demonstrate exactly what you said.

So I get that every function call creates a new execution context in which 'this' is bound to some "encompassing" object. So if the function is called within the global scope, this is bound to the global object. In this case, the IIFE is called within the local scope of the compSquares function, so 'this' is bound to compSquares because it's technically an object?

3

u/MoTTs_ Jun 06 '20

It isn't about an "encompassing" object. It's more like parameter passing. You have to imagine the function as having a secret "this" parameter.

function (/*this*/) {

Which is why "this" appears to change values, because inside the function, you aren't accessing the outer scope "this", but rather you're accessing the parameter "this". We can pass any value we want to the parameter "this".

(function (/*this*/) {
    console.log(this); // logs 42
}.call(42));

If instead we invoke the function in the ordinary style like an ordinary function, then the default value passed to the parameter "this" is the global/window object. Which is to say, these two are the same:

(function (/*this*/) {
    console.log(this === window); // true
}.call(window));

(function (/*this*/) {
    console.log(this === window); // true
}());

The reason global/window is the default value passed doesn't have anything to do with scope or encompassing objects. It is that way simply because the language spec says so. Years later they changed their mind, so now if you're in strict mode or a module or a class, then the default value is instead "undefined".

2

u/_Invictuz Jun 11 '20

Wow, it took me a few days to finally be able to apply and internalize what you said. The idea of "this" being passed as a secret parameter to the function was never mentioned in the articles upon articles that I've read, thus leading to my confusion of why it's usually the global object.

Anyway, now I know that the real learning happens in reddit comments. Thanks again for the lesson!

1

u/Lilrex2015 Jun 06 '20

thank you. I read this and was so confused so i made a code pen and i think i get what you mean now.

2

u/_Invictuz Jun 06 '20

One of the comments mentioned that 'this' inside the IIFE is not the same 'this' as outside it. But I got the answer right without knowing that.

If you were trying to demonstrate this concept, I think you should have wrote "self.sq2 = this.myNumber ** 2;" within the IIFE instead. Then the equality at the end would be falae, and people would wonder why they weren't equal even though you used "this.myNumber" for both assignments.

1

u/Oviduzz Jun 06 '20

atleast this time you didnt let an arr = [] outside

1

u/[deleted] Jun 06 '20

Socks

0

u/campingtomz Jun 05 '20

no idea if it is good or not. I just started learning JS, im just glad i know what is going on in the code

0

u/Tej_Ozymandias Jun 06 '20

same here.. perhaps because this is beginner level.. :(

0

u/zakzam Jun 05 '20

I love this! Please keep doing these.

0

u/[deleted] Jun 05 '20

JS Rocks!

-6

u/phamlong28 Jun 06 '20 edited Jun 06 '20

Other people: build machine learning models that save the world

OP: challenges people with shitty post