DEV Community

Justin
Justin

Posted on

JS Challenge: Check if an array has all its values sorted in the ascending order

We can use every() method of array to check if condition is true for every element of the array. So our solution using every() method goes like this:

arr1 = [1, 2, 3, 4, 6, 5];
arr2 = [1, 2, 3, 4, 5, 6];

arr1.every((element, index, array) => 
  index === 0 || element >= array[index - 1]
)

arr2.every((element, index, array) => 
  index === 0 || element >= array[index - 1]
)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)