diff --git a/README.md b/README.md index 48d8f69ec..d39a1c77d 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ * [`initializeArrayWithRange`](#initializearraywithrange) * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) +* [`isArraySorted`](#isarraysorted) * [`join`](#join) * [`last`](#last) * [`mapObject`](#mapobject) @@ -921,6 +922,38 @@ intersection([1, 2, 3], [4, 3, 2]); // [2,3]
[⬆ Back to top](#table-of-contents) +### functionName + +Returns `1` if the array is sorted in ascending order, `-1` if it is sorted in descending order or `0` if it is not sorted. + +Calculate the ordering `direction` for the first two elements. +Use `Object.entries()` to loop over array objects and compare them in pairs. +Return `0` if the `direction` changes or the `direction` if the last element is reached. + +```js +const isArraySorted = arr => { + const direction = arr[0] > arr[1] ? -1 : 1; + for (let [i, val] of arr.entries()) + if (i === arr.length - 1) return direction; + else if ((val - arr[i + 1]) * direction > 0) return 0; +}; +``` + +
+Examples + +```js +isArraySorted([0, 1, 2, 3]); // 1 +isArraySorted([0, 1, 2, 2]); // 1 +isArraySorted([4, 3, 2]); // -1 +isArraySorted([4, 3, 5]); // 0 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### join Joins all elements of an array into a string and returns this string. Uses a separator and an end separator. diff --git a/docs/index.html b/docs/index.html index f800e8e90..d0ef902a0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -163,6 +163,16 @@ initializeArrayWithRange(7, 3); // [3,4,5,6,7]
   return a.filter(x => s.has(x));
 };
 
intersection([1, 2, 3], [4, 3, 2]); // [2,3]
+

functionName

Returns 1 if the array is sorted in ascending order, -1 if it is sorted in descending order or 0 if it is not sorted.

Calculate the ordering direction for the first two elements. Use Object.entries() to loop over array objects and compare them in pairs. Return 0 if the direction changes or the direction if the last element is reached.

const isArraySorted = arr => {
+  const direction = arr[0] > arr[1] ? -1 : 1;
+  for (let [i, val] of arr.entries())
+    if (i === arr.length - 1) return direction;
+    else if ((val - arr[i + 1]) * direction > 0) return 0;
+};
+
isArraySorted([0, 1, 2, 3]); // 1
+isArraySorted([0, 1, 2, 2]); // 1
+isArraySorted([4, 3, 2]); // -1
+isArraySorted([4, 3, 5]); // 0
 

join

Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.

Use Array.reduce() to combine elements into a string. Omit the second argument, separator, to use a default separator of ','. Omit the third argument, end, to use the same value as separator by default.

const join = (arr, separator = ',', end = separator) =>
   arr.reduce(
     (acc, val, i) =>
diff --git a/snippets/isArraySorted.md b/snippets/isArraySorted.md
index 005b4be78..e054697c2 100644
--- a/snippets/isArraySorted.md
+++ b/snippets/isArraySorted.md
@@ -9,15 +9,15 @@ Return `0` if the `direction` changes or the `direction` if the last element is
 ```js
 const isArraySorted = arr => {
   const direction = arr[0] > arr[1] ? -1 : 1;
-  for(let [i, val] of arr.entries())
+  for (let [i, val] of arr.entries())
     if (i === arr.length - 1) return direction;
     else if ((val - arr[i + 1]) * direction > 0) return 0;
-}
+};
 ```
 
 ```js
-isArraySorted([0,1,2,3]) // 1
-isArraySorted([0,1,2,2]) // 1
-isArraySorted([4,3,2]) // -1
-isArraySorted([4,3,5]) // 0
+isArraySorted([0, 1, 2, 3]); // 1
+isArraySorted([0, 1, 2, 2]); // 1
+isArraySorted([4, 3, 2]); // -1
+isArraySorted([4, 3, 5]); // 0
 ```