this post was submitted on 08 Nov 2022
5 points (100.0% liked)

Programmer Humor

32078 readers
651 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

If an element is out of order, remove it from the array.

An example in everyone's (least) favorite language:

const americanSort = (list, compare) => list.reduce((acc, cur) => { const lastElement = acc.length > 0 ? acc[acc.length - 1] : null; return lastElement ? compare(lastElement, cur) < 1 ? [...acc, cur] : acc : [cur]; }, []);

console.log(americanSort([5, 2, 6, 5, 20, 10, 40], (a, b) => a - b)) outputs Array(4) [ 5, 6, 20, 40 ]

you are viewing a single comment's thread
view the rest of the comments
[–] muad_dibber@lemmygrad.ml 3 points 2 years ago

I feel like the americanSort would be to remove all the numbers and replace them with the same number repeating forever.

americanSort([55, 331, 26, 89]) -> [1, 1, 1, 1, 1, 1, 1, ...]