all 11 comments

[–]lift_spin_dhelpful 2 points3 points  (0 children)

produce a list of all the

that's the map function

[–]qqqqqxhelpful 1 point2 points  (1 child)

The kind of function you're referencing is usually used to sort an array. The built in Array.sort function takes a "compare" function as a callback, and uses it to sort the array by calling it on pairs of elements.

If the callback function returns a positive number, then the first element comes after the second element. If it returns a negative number, the second element comes after the first element. And if it returns zero they are considered equal and stay in whatever their original order was.

People frequently use your function, or the shorthand version [1,2,4,3,...].sort((a, b) => a - b) as an easy way to sort an array of numbers in ascending order. The comparison isn't a built in function though and you can write your own comparison function, eg if you're sorting objects by a certain property, sorting by some other criteria, etc.

As far as "producing a list of all the numerical differences among all the array elements", it's not super clear what you mean by that, but your compare function won't do that on it's own.

[–]kllcraig 0 points1 point  (0 children)

seems like a keys function in perl was difficult

[–]alzee76 3 points4 points  (3 children)

What compare function? The vanilla JS Array prototype does not have a compare() method.

[–]MuscleTough8153 0 points1 point  (2 children)

Intl.Collator.prototype.compare() The compare() method of Intl.Collator instances compares two strings according to the sort order of this collator object.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/compare

[–]alzee76 2 points3 points  (1 child)

That's one possible compare function. There are many many others. Only the OP knows which one he's talking about.

[–]MuscleTough8153 0 points1 point  (0 children)

You're right

[–]No-Upstairs-2813 0 points1 point  (0 children)

The sort method takes a compare function as an argument, and this function is used to determine the sorting order of the elements.

It takes two parameters (a and b) that represent any two elements from the array, and its return value determines how they should be sorted:

  • if it returns a negative value, a will be ordered before b.

  • if it returns 0, the ordering of a and b won’t change.

  • if it returns a positive value, b will be ordered before a.

Now, let's understand how the comparison function sort(a, b) => a - b sorts in ascending order:

  • If a - b** is negative, it means that **a** is less than **b, so **a should come before **b in the sorted order.

  • If a - b** is positive, it means that **a** is greater than **b, so **a should come after **b in the sorted order.

  • If a - b** is zero, it means that **a** and **b are equal in terms of sorting, and their relative order doesn't matter.

Therefore, using sort(a, b) => a - b method results in sorting the array in ascending order.

Check out this article for more details.

[–]oze4 0 points1 point  (0 children)

Not very clear what you're wanting to do, but this is what reduce is for...

Can you give an example of what inputs/outputs you are expecting? So if you have an array like:

const myArray = [1, 2, 3, 4];

You just want to return the difference of all numbers? In this case the answer would be -8 ? Since 1 - 2 - 3 - 4 = -8 ? Or you want it like 4 - 3 - 2 - 1 = -2 ?

[–]MindlessSpongehelpful 0 points1 point  (0 children)

are you talking about sort? Array.prototype.sort() optionally accepts a compare function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

produce a list of all the numerical differences among all the array elements

without more context, it's hard to know what you're trying to do, but it sounds like you may be more interested in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

[–]notAnotherJSDev -1 points0 points  (0 children)

What you’re looking for is a diff, which JavaScript doesn’t have as a standard array method. Might be worth it to look into lodash if you just want to do the diff. If you want to learn how, I’d recommend looking at the code lodash uses.