r/badcode • • Jul 03 '19

js Found on stack overflow, The most efficient way to remove particular array elements

Post image
811 Upvotes

58 comments sorted by

78

u/Webmets Jul 03 '19

Javascript has its reputation for a reason, with the average user coding this shit

/s

133

u/Qesa Jul 03 '19

A true javascript developer would import a node module to filter 5s.

72

u/Webmets Jul 03 '19

And that package comes with 3GB of dependencies

0

u/[deleted] Jul 03 '19

[deleted]

12

u/sdrrhmni Jul 03 '19

Lol, this is hilarious

41

u/husao Jul 03 '19

// PLEAS READ THE SOURCE CODE BECAuse we moved fast and broke things

hmm k

https://github.com/jezen/is-thirteen/blob/master/is.js

pretty good

https://github.com/jezen/is-thirteen/blob/master/consts.js

perfection

26

u/gangqiu0 Jul 03 '19

" // B just looks like 13 written closer"

Give him a raise

2

u/mudkip908 Jul 04 '19

I like how Icelandic and Polish have so many variations of "thirteen"

1

u/Examo Jul 04 '19

This is sarcasm

1

u/[deleted] Jul 07 '19

10/10 has Belter Creole.

21

u/ahk-_- Jul 03 '19

have you met my friend, isnt-thirteen

0

u/[deleted] Jul 03 '19

[deleted]

17

u/Keroths Jul 03 '19

As you can see it's dead serious.

Without it the internet wouldn't exist as we know it so you better respect those 500+ commits.

8

u/jokullmusic Jul 03 '19

yeah I'm pretty sure it's originally a r/programmingcirclejerk joke

192

u/modeler Jul 03 '19

Now try that with [15,5,4,3,2,1]

Yuck. Now I need to take a shower.

79

u/ahk-_- Jul 03 '19
let array = [5, 5, 4, 4, 2, 3, 4]
let newArray = array.every(x => !isNaN(x)) ? ("," + array.join(",")).
                                    replace(" ", "").
                                    replace(",5", "").
                                    slice(1).split(",").
                                    map(x => parseInt(x)) : null

54

u/ScriptingInJava Jul 03 '19

it just works

19

u/CodenameLambda shameless Jul 03 '19

What about [3, 50, 4]?

57

u/ahk-_- Jul 03 '19

Thanks for posting the issue. I'll make sure to merge it once it passes the pipeline.

let array = [5, 15, 50, 4, 4, 2, 3, 4, 5]
let newArray = array.every(x => !isNaN(x)) ? ("," + array.join(",") + ",").
                                    replace(" ", "").
                                    replace(",5,", ",").
                                    slice(1, -1).split(",").
                                    map(x => parseInt(x)) : null

60

u/[deleted] Jul 03 '19

get that shit on NPM

34

u/cdrootrmdashrfstar Jul 03 '19

production ready

7

u/[deleted] Jul 04 '19

How can I be sure this works if there are no unit tests?

7

u/ahk-_- Jul 04 '19
 function deleteItem(array, item){
  return array.every(x => !isNaN(x)) && !isNaN(item) ? ("," + array.join(",") + ",").
  replace(" ", "").
    replace(","+item.toString()+",", ",").
    slice(1, -1).split(",").
    map(x => parseInt(x)) : null
}

Tests

testArrays = [[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [15, 12, 123, 5, 6],
              [50, 5, 3, 2, 1],
              [5, 15, 50, 34, 23]]
if ( !(deleteItem(testArrays[0], 5)    .toString() == [1, 2, 3, 4].toString())) throw "Test 1 failed";
if ( !(deleteItem(testArrays[1], 5).toString() == [4, 3, 2, 1].toString())) throw "Test 2 failed";
if ( !(deleteItem(testArrays[2], 5).toString() == [15, 12, 123, 6].toString())) throw "Test 3 failed";
if ( !(deleteItem(testArrays[3], 5).toString() == [50, 3, 2, 1].toString())) throw "Test 4 failed";
if ( !(deleteItem(testArrays[4], 5).toString() == [15, 50, 34, 23].toString())) throw "Test 5 failed";

-1

u/CodenameLambda shameless Jul 03 '19

Cute

5

u/jocull Jul 03 '19

Needs more npm

2

u/Blackwater_7 Jul 04 '19

ATTENTION ATTENTION

PAY FOR LINE WORKER DETECTED

29

u/HumblesReaper Jul 03 '19

Or strings with a comma inside

2

u/franz_bonaparta_jr Jul 04 '19

Silly programmer, you can’t have such complicated numbers

94

u/FergusInLondon Jul 03 '19

I mean, I'm actually pretty impressed. It's certainly creative!

21

u/Webmets Jul 03 '19

Though not functional

14

u/leaningtoweravenger Jul 03 '19

That's the real language of the future!

I mean, one of those post-atomic futures when people will kill each other for few drops of gasoline and most probably also the reason why we will end up there /s

8

u/[deleted] Jul 03 '19

The future is already here. I write financial software in JS... :'(

7

u/jocull Jul 03 '19

Floating point errors - No big deal!

3

u/[deleted] Jul 04 '19

"It's handled by the database" Suuure...

2

u/Prom3th3an Jul 07 '19

In post-atomic futures, are "post-atomic futures" a concurrency-control tool?

24

u/[deleted] Jul 03 '19

the -11 isn't the number of downvote, it's that guy iq

2

u/Webmets Jul 03 '19

Probably both

7

u/BobFrAccounting Jul 03 '19

Give this guy a map function....

8

u/BobFrAccounting Jul 03 '19 edited Jul 03 '19
const removeElementFromArray = (item, array) => {
  return array.map((current) => current !== item && item)
}
const array = [5, 5, 4, 4, 2, 3, 4]
const newArray = removeElementFromArray(5, array)

6

u/stratoscope Jul 03 '19 edited Jul 03 '19

Oh, I like that, who needs to actually remove the array elements when you can just replace them with false like this code does (after the missing parenthesis is fixed)?

More seriously, I have seen many developers think map is an all-purpose function and use it instead of filter or forEach or the like. map always produces an output array the same length as the input.

8

u/BobFrAccounting Jul 03 '19 edited Jul 03 '19

I didn't think I would need a /s on this post. This code literally replaces the first two 5's with false, and then the rest of the array with 5's. Jokes are less funny when you have to explain them, tho. Was hoping someone would just run it in the web console and giggle.

Also, I don't really like using `map`, `filter`, or `forEach` for these types of problems. Would rather actually use `reduce` to take the array, and reduce it into a new array with the values we want to keep. But I wanted to make a funny, and am a Basketball fan. (Hence the 'get this guy a map' reference, which is a common phrase used to make fun of a person who fell for a dribble move, or pump-fake. It's used more broadly for more types of sports ball, but I am mainly a basketball fan...so, meh.)

3

u/stratoscope Jul 03 '19

Yes, I edited my comment a couple of minutes after posting it because I realized you had to have been joking. Sorry for my initial confusion - after reading some of the other bad answers that were posted in all seriousness on SO, I didn't know what to believe any more! :-)

2

u/BobFrAccounting Jul 03 '19

SO can be a source of all manner of unintentional comedic genius.

2

u/Voltra_Neo Jul 03 '19
let array = [5,5,4,4,2,3,4];
let newArray = array.join("").replace("5", "").split("");

I'm the devil now

2

u/raiyyansid Regex Jul 03 '19

let newArray = array.filter(num => num !== 3);

2

u/jbondhus Jul 03 '19

Does anyone have a link to the stack overflow post?

6

u/stratoscope Jul 03 '19 edited Jul 03 '19

Now prepare to be terrified: the SO contributor who posted this awful answer has a reputation of 14,121 (and falling) and says "I am a Software Developer at a major ERP product company."

(Edit: My apology to that contributor! As two replies pointed out, he just edited the answer to improve the code formatting and didn't write it. OTOH, I am a little surprised that he only edited it but didn't add a comment about how wrong it was.)

More seriously, the entire SO thread is full of people jumping in with "solutions" without even asking what the OP even meant by their question:

I have an array of Numbers, and I'm using the .push() method to add elements to it.

Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(number);.

I have to use core JavaScript - no frameworks are allowed.

Note that there is no indication of what this array.remove function is actually supposed to do. Is number an index into the array? Is it a value to be found within the array? Or what?

The question has three pages of answers, which all make different assumptions about this, and many of which suggest extending the Array prototype - which is almost always a bad idea.

And the question itself - which is a good example of an awfully underspecified question - has 7229 points!

10

u/Zerquix18 Jul 03 '19

The guy who posted actually has a reputation of 3 points.
The guy who edited it, however, has a reputation of 14k.

2

u/dombrogia Jul 03 '19

Finally some bad code, not some “my function name from 6 months ago could have been better” screenshots.

1

u/Santoroma17 Jul 03 '19

Holy hell...

1

u/blankman0230 Jul 04 '19

Been there, done that.

1

u/Smiley1000YT Jul 20 '19

Anyone dare to explain how this code is supposed to work?

1

u/GideonMax Sep 06 '19

You should just use the splice command

0

u/Dazeeeh Jul 03 '19

What is splice anyway? Sounds weird.