Here is a problem I can't manage, it comes from codingGame. I saw a solution but I tried to use a different syntax in order to better understand.
Context
/** Horse Racing Duals (easy) https://www.codingame.com/training/easy/horse-racing-duals
* This puzzle shows that sometimes, the simplest solution is not performant
* enough. You will also learn to handle large arrays and gain experience
* with their processing time.
*
* STATEMENT:
* In this problem you have to find the two numbers that are closest to each
* other among a list of numbers. The list might be really large and force
* you to search for the best possible algorithmic complexity for your
* solution.
*
* STORY:
* To make a race between two horses interesting, you will have to find the
* horses with the closest strength.
**/
print(
new Array(+readline())
.fill()
.map(() => +readline())
.sort((a, b) => a > b)
.map((x, i, arr) => Math.abs(x - arr[i + 1]))
.sort((a, b) => a > b) [0]
);
Why can't I do this:
let tab = new Array(+readline())
tab.fill()
tab.map(() => +readline())
tab.sort((a, b) => a > b)
tab.map((x, i, arr) => Math.abs(x - arr[i + 1]))
tab