https://www.codewars.com/kata/5b73fe9fb3d9776fbf00009e
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
Your task is to sum the differences between consecutive pairs in the array in descending order.
Example
[2, 1, 10] --> 9
In descending order: [10, 2, 1]
Sum: (10 - 2) + (2 - 1) = 8 + 1 = 9
If the array is empty or the array has only one element the result should be 0 (Nothing in Haskell, None in Rust).
코드
function sumOfDifferences(arr) {
arr.sort((a, b) => b - a);
return arr[0] - arr[arr.length - 1] || 0;
}
댓글