r/javascript • • Mar 24 '18

Can't keep Array.shift vs Array.unshift straight? (NSFW) NSFW

Here is a helpful, although gross mnemonic to help keep the two straight in your head. Drop the "f".

So, Array.shit() drops a load (element) from the beginning of an array and Array.unshit() will do the opposite.

69 Upvotes

31 comments sorted by

73

u/Skhmt Mar 24 '18

This will confuse me, since you don't shit from the head, you shit from the back. But Array.shit() comes from the head.

34

u/goto-reddit Mar 24 '18

Just remember this brilliant idea from Cartman as well:

If you eat food, you crap out your butt, right? Yeah Alright, now keep with me here, it gets a little complicated If you eat food and crap out your butt, Then maybe if you stuck food in your butt You could crap out your mouth.

South Park Season 06 Episode 08 - Red Hot Catholic Love

0

u/[deleted] Mar 25 '18

[deleted]

6

u/mastermindxs Mar 25 '18

And these are just the good parts.

2

u/juampi92 Mar 24 '18

Hahaha my thoughts exactly

1

u/potchie626 Mar 24 '18

Cartman would say otherwise.

-6

u/Cheezmeister http://bml.rocks Mar 24 '18

Just imagine the array is Donald Drumpf. All that comes out of its mouth is a bunch of shit.

1

u/partusman Mar 24 '18

That’s more of a comment, really.

1

u/backltrack Apr 01 '18

Drump DESTROYED

-3

u/[deleted] Mar 24 '18

Kind of a cheesy joke tbh, but I auto-upvote anything anti-Trump so you got mine. Honestly, the worse the joke, the more I want to upvote it. There's something hilarious seeing all the whiners complaining about how stuff gets upvoted for no other reason than being anti-Trump.

38

u/senocular Mar 24 '18

I used the length to help remember.

push is longer than pop because it adds
pop is shorter than push because it removes
unshift is longer than shift because it adds
shift is shorter than unshift because it removes

Not as fun as dropping the "f" tho :(

3

u/tunnckoCore node-formidable, regexhq, jest, standard-release Mar 24 '18

Interesting.

2

u/frambot Mar 24 '18

This is super helpful, thank you!

12

u/mauvm Mar 24 '18

Why is this NSFW?

11

u/r2d2_21 Mar 24 '18

Because shit? I see no other reason.

0

u/[deleted] Mar 24 '18

[deleted]

26

u/beb1312 Mar 24 '18

In my workplace its not uncommon to hear "fuck your mother" being directed toward a computer.

4

u/NotSelfAware Mar 25 '18

I once punched my monitor at work out of frustration, and it broke. It was old so they didn't care much. Pretty soon after we all went out for lunch.

12

u/[deleted] Mar 24 '18 edited Mar 24 '18

the word "shit" is generally not acceptable in the workplace here in the United States.

If you think that's really the case, then maybe you work with a bunch of puritans.

This post may be 'nsfw' entirely because there are minors interested in javascript.

No adult I know in any place I've worked would be offended by the word "shit".

2

u/mauvm Mar 24 '18

Any idea why?

3

u/oneeyedziggy Mar 25 '18

depending on the workplace a lot of us yanks are prudes, fancy ourselves "good Christians" and otherwise delight in upholding a bunch of made-up bs rules... makes the weak feel more powerful

2

u/oneeyedziggy Mar 25 '18

your workplace sucks then... you do what you gotta do to feed your family, but think of looking for somewhere less stuckup... as long as you're not swearing AT people, rules like that just show your workplace has its priorities all out of whack... what other usless or even obstructionist bs rules are they holding over you... you wear a uniform to your non-customer-interaction deskjob too? I just wish more people demanded better of their employers

3

u/prof3ssorSt3v3 Web|Mobile Design|Dev Mar 24 '18

My students will appreciate this. :)

But it is drop the "f".

3

u/freebit Mar 24 '18

Thanks! Fixed. Haven't had enough coffee yet. :)

8

u/sh0rtwave Mar 24 '18 edited Mar 25 '18

Some day, we'll get it right and realize we just need clear semantics for what these damned things actually do.

Teaching array.pop/push/slice/shift/unshift is one of the toughest things to do, IMHO.

Everbody gets pop and push, instantly. They make sense. We all know about popping something off the top of a stack of dishes and pushing something else back on top of it.

So does "slice". (Who doesn't understand slicing a pie?). We're just taking SOME of the dishes out of the stack, and maybe sticking new ones in. Makes sense. Lift the top part of the stack you're leaving, stick something new in, take something out, set them back down. Easy-peasy.

In fact, almost all the other functions are simple to understand, because they relate to physical things we can understand.

So that stack of plates...how does unshift work? So really, what you're doing is lifting the entire stack, and then...un-shifting(carefully scooting) the bottom plate with your foot into place so you can put that big stack back down on top of it.

Shift? Lift that whole stack of plates with both hands, and kick that bottom plate out with vigor.

I find, when explaining things to people about computing, it's best if you can grab an idea of the physical act you're doing, it aids with understanding in a whole other way than just being a memory tool.

Edit: I should mention...it took me a year or so of teaching(adults) to come up with that. Some of my students (mostly adult) had serious problems grasping even things like variables. Seriously, I had to use cups + water + food coloring to simulate the idea that a variable can NOT hold two values at a time. (That came in useful in many situations, e.g. AoCP Q#1.)

Edit again: Don't even get me started about trying to explain bitmasks.

Edit again again: Explained it backwards like an idiot.

3

u/[deleted] Mar 24 '18

Unshift? Lift that whole stack of plates with both hands, and kick that bottom plate out with vigor.

But unshift adds, so you’d be carefully sliding a plate under.

3

u/sh0rtwave Mar 25 '18

Right, I even got it backwards explaining it.

1

u/Gemmellness Mar 25 '18

Shift moves the front pointer along one. Unshift moves it back one.

1

u/sh0rtwave Mar 25 '18

The technical explanation aside... This is how I explained it to absolute neophytes.

1

u/filleduchaos Mar 25 '18

...or maybe don't try to explain an Array(List) as though it's a Stack?

The methods are named just fine.

1

u/sh0rtwave Mar 25 '18

I never said they were named badly. Just that the explanation was easier to understand in physical terms.

3

u/SexyIntelligence Mar 24 '18

And here I was, expecting to see Javascript porn...

2

u/darkde Mar 24 '18

I just memorized that adding methods have a 'u' and removing doesnt