BogoSort.js: Simplify Array.isSorted() and add doctests

As discussed at https://github.com/TheAlgorithms/Javascript/issues/164#issuecomment-627437233

Also related to add doctests as discussed in #142
This commit is contained in:
Christian Clauss
2020-05-12 22:21:36 +02:00
committed by GitHub
parent 86b3622536
commit 02585f5d12

View File

@ -3,14 +3,17 @@
* sorted in ascending order.
*/
// > [].isSorted()
// true
// > [1].isSorted()
// true
// > [1,2,3].isSorted()
// true
// > [3,2,1].isSorted()
// false
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
Array.prototype.isSorted = function () {
const length = this.length
if (length < 2) {
return true
}
for (let i = 0; i < length - 1; i++) {
if (this[i] > this[i + 1]) {
return false