function isSorted(arr) { const limit = arr.length - 1; return arr.every((_, i) => (i < limit ? arr[i] <= arr[i + 1] : true)); } The function works, as I understand it and I do not understand:
i<limittells us whether we have reached the end of the array or not, if we have reached it, it indicates that the array is sorted (true), if it has not reached it, it moves to the next element of the arrayarr[i] <= arr[i + 1]arr.everychecks whether all elements of the array passed the specified conditionwhat does
(_, i) =>
I ask you to answer the 4th point as clearly as possible, I can almost imagine what is happening here, but not quite yet, it would be nice to show what piece of code you can replace the code in paragraph 4. And if I was mistaken in explaining other points, too please correct. Thank.
everymethod takes as its first argument the current processed element of the array, but for the task only the second argument was needed - the index of this element. Without the first argument, the second will not get. Therefore, the author of the code marked the first as an “insignificant” underscore. - Deonis