Imagine you have a list of numbers, and you want to know if they’re arranged in a way that makes sense—specifically, if they’re sorted from smallest to largest, or at least not decreasing as you move through the list.
Your Task:
Write a function that checks if the numbers in a given list are in non-decreasing order. This means each number in the list should be less than or equal to the number that follows it.
Input: array = [1, 2, 2, 3, 4]
Output: true
Input: array = [10, 20, 30, 25, 40]
Output: false
Input: array = [3, 2, 1]
Output: false
Input: string = "hello"
Output: Throw error => Input is not a valid array
function isArraySortedLoop(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
for (let i = 1; i < array.length; i++) {
if (array[i - 1] > array[i]) {
return false;
}
}
return true;
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedLoop(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedLoop(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedLoop(array3); //output: false
const string4 = "hello";
isArraySortedLoop(string4); //Throw error => Input is not a valid array
A hash map (or dictionary) is a great way to solve this problem eff…
function isArraySortedEvery(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
return array.every((value, index, arr) => index === 0 || arr[index - 1] <= value);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedEvery(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedEvery(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedEvery(array3); //output: false
const string4 = "hello";
isArraySortedEvery(string4); //Throw error => Input is not a valid array
function isArraySortedReduce(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
return array.reduce((sorted, value, index) => {
return sorted && (index === 0 || array[index - 1] <= value);
}, true);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedReduce(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedReduce(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedReduce(array3); //output: false
const string4 = "hello";
isArraySortedReduce(string4); //Throw error => Input is not a valid array
function isArraySortedForEach(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
let sorted = true;
array.forEach((value, index) => {
if (index > 0 && array[index - 1] > value) {
sorted = false;
}
});
return sorted;
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedForEach(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedForEach(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedForEach(array3); //output: false
const string4 = "hello";
isArraySortedForEach(string4); //Throw error => Input is not a valid array
function isArraySortedSome(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
return !array.some((value, index) => index > 0 && array[index - 1] > value);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedSome(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedSome(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedSome(array3); //output: false
const string4 = "hello";
isArraySortedSome(string4); //Throw error => Input is not a valid array
function isArraySortedFind(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
return array.find((value, index) => index > 0 && array[index - 1] > value) === undefined;
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedFind(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedFind(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedFind(array3); //output: false
const string4 = "hello";
isArraySortedFind(string4); //Throw error => Input is not a valid array
function isArraySortedMap(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
return !array.map((value, index) => index > 0 && array[index - 1] > value).includes(true);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedMap(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedMap(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedMap(array3); //output: false
const string4 = "hello";
isArraySortedMap(string4); //Throw error => Input is not a valid array
function isArraySortedRecursive(array, index = 0) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
if (index >= array.length - 1) return true;
if (array[index] > array[index + 1]) return false;
return isArraySortedRecursive(array, index + 1);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedRecursive(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedRecursive(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedRecursive(array3); //output: false
const string4 = "hello";
isArraySortedRecursive(string4); //Throw error => Input is not a valid array
function isArraySortedJSON(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
const sortedArray = [...array].sort((a, b) => a - b);
return JSON.stringify(array) === JSON.stringify(sortedArray);
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedJSON(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedJSON(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedJSON(array3); //output: false
const string4 = "hello";
isArraySortedJSON(string4); //Throw error => Input is not a valid array
function isArraySortedByComparison(array) {
if (!Array.isArray(array)) {
throw new Error('Input is not a valid array');
}
const sortedArray = [...array].sort((a, b) => a - b);
return arraysAreEqual(array, sortedArray);
}
function arraysAreEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
const array1 = [1, 2, 2, 3, 4];
isArraySortedByComparison(array1); //output: true
const array2 = [10, 20, 30, 25, 40];
isArraySortedByComparison(array2); //output: false
const array3 = [3, 2, 1];
isArraySortedByComparison(array3); //output: false
const string4 = "hello";
isArraySortedByComparison(string4); //Throw error => Input is not a valid array