mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
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:
@ -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
|
||||
|
Reference in New Issue
Block a user