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:

  1. i<limit tells 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 array

  2. arr[i] <= arr[i + 1]

  3. arr.every checks whether all elements of the array passed the specified condition

  4. what 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.

  • Completed the answer in more detail - Dmitry Kozlov
  • To the answers below, you can also add about the underscore character. Not infrequently, thus denote an argument that must be present, for example, in the case of the order prescribed by the syntax, but does not participate in the script itself. The every method 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

3 answers 3

"_" is just a symbol, no different from others.

what does (_, i) =>

In this case, "_" is the current element of the array passed to the callback function, i is its index

 arr.every((_, i) => (i < limit ? arr[i] <= arr[i + 1] : true)); 

In this case, the entire array is searched and it is checked that the current element is less than or equal to the next one. Those. array sorted. When elements end, true is returned.

The every () method calls the passed callback function once for each element present in the array until it finds one for which the callback returns a false value (a value that becomes false when it is coerced to Boolean). If such an element is found, the every () method will immediately return false. Otherwise, if callback returns true for all elements of the array, the every () method returns true. The callback function is called only for array indices that have assigned values; it is not called for indexes that have been deleted or to which values ​​have never been assigned.

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/every

    JavaScript underlining has no "magic properties". Just the author so called the parameter of an anonymous function.

    The every method calls for each element of the array the function transferred to it. The transferred function can take up to 3 parameters - the ЗначениеТекущегоЭлемента ПорядковыйНомерЭтогоЭлемента , the ПорядковыйНомерЭтогоЭлемента , and the ПеребираемыйМасив . As a result, this function should return true or false . If all calls to the function returned are true , every will return true . If at least one call returns false - every returns false .

    If you use the usual loop, then the similar code will look like this:

     function isSorted(arr) { for ( int i = 0; i < arr.length - 1 ; i++ ) { if ( arr[i] > arr[i+1] ) { return false; } } return true; } 

      You can simplify the function:

       const arr = [1, 2, 3] const arr2 = [3, 2, 1] function isSorted (arr) { return arr.slice(1).every((item, i) => arr[i] >= item) } console.log(isSorted(arr), isSorted(arr2)) 

      If you need a cycle:

       const arr = [1, 2, 3] const arr2 = [3, 2, 1] function isSorted (arr) { for (let i in arr.slice(1)) if (arr[i - 1] < arr[i]) return false return true } console.log(isSorted(arr), isSorted(arr2))