From 02585f5d12ff7995e05d89a007efd7ea6cf9c044 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 12 May 2020 22:21:36 +0200 Subject: [PATCH] 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 --- Sorts/BogoSort.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index f8961bb18..68d68edcc 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -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