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
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.
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
3
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 = thisbecause in the IIFE,thisdoes not refer to the same thing asthisin 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
1
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
0
0
-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
49
u/ForScale Jun 05 '20
Rocks.
But kind of shitpost. :)