r/badcode • u/sdrrhmni • Jul 03 '19
js Found on stack overflow, The most efficient way to remove particular array elements
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)) : null54
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)) : null60
34
7
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
5
2
29
2
94
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
Jul 03 '19
The future is already here. I write financial software in JS... :'(
7
2
u/Prom3th3an Jul 07 '19
In post-atomic futures, are "post-atomic futures" a concurrency-control tool?
24
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
falselike this code does (after the missing parenthesis is fixed)?More seriously, I have seen many developers think
mapis an all-purpose function and use it instead offilterorforEachor the like.mapalways 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
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
2
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
1
1
1
0
78
u/Webmets Jul 03 '19
Javascript has its reputation for a reason, with the average user coding this shit
/s