diff --git a/README.md b/README.md index a18303d29..2aabfdefe 100644 --- a/README.md +++ b/README.md @@ -1156,7 +1156,7 @@ Omit the second argument, `depth` to flatten only to a depth of `1` (single flat ```js const flatten = (arr, depth = 1) => - depth != 1 + depth !== 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), []) : arr.reduce((a, v) => a.concat(v), []); ``` @@ -1517,9 +1517,9 @@ 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) => - i == arr.length - 2 + i === arr.length - 2 ? acc + val + end - : i == arr.length - 1 ? acc + val : acc + val + separator, + : i === arr.length - 1 ? acc + val : acc + val + separator, '' ); ``` @@ -1962,7 +1962,7 @@ const remove = (arr, func) => Examples ```js -remove([1, 2, 3, 4], n => n % 2 == 0); // [2, 4] +remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4] ``` @@ -3956,7 +3956,7 @@ const negate = func => (...args) => !func(...args); Examples ```js -[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 == 0)); // [ 1, 3, 5 ] +[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 === 0)); // [ 1, 3, 5 ] ``` @@ -4595,7 +4595,7 @@ Return `false` if any of them divides the given number, else return `true`, unle ```js const isPrime = num => { const boundary = Math.floor(Math.sqrt(num)); - for (var i = 2; i <= boundary; i++) if (num % i == 0) return false; + for (var i = 2; i <= boundary; i++) if (num % i === 0) return false; return num >= 2; }; ``` @@ -4806,7 +4806,7 @@ const primes = num => { let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2), sqroot = Math.floor(Math.sqrt(num)), numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2); - numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y == x))); + numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x))); return arr; }; ``` diff --git a/docs/index.html b/docs/index.html index da348c4a1..4b5bd541f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -185,7 +185,7 @@ Object.assig .slice(-1)[0][0];
findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2 (index of the value 3)
 

flatten

Flattens an array up to the specified depth.

Use recursion, decrementing depth by 1 for each level of depth. Use Array.reduce() and Array.concat() to merge elements or arrays. Base case, for depth equal to 1 stops recursion. Omit the second argument, depth to flatten only to a depth of 1 (single flatten).

const flatten = (arr, depth = 1) =>
-  depth != 1
+  depth !== 1
     ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), [])
     : arr.reduce((a, v) => a.concat(v), []);
 
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
@@ -255,9 +255,9 @@ Object.assig
 

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) =>
-      i == arr.length - 2
+      i === arr.length - 2
         ? acc + val + end
-        : i == arr.length - 1 ? acc + val : acc + val + separator,
+        : i === arr.length - 1 ? acc + val : acc + val + separator,
     ''
   );
 
join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); // "pen,pineapple,apple&pen"
@@ -375,7 +375,7 @@ Object.assig
         return acc.concat(val);
       }, [])
     : [];
-
remove([1, 2, 3, 4], n => n % 2 == 0); // [2, 4]
+
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
 

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it of to the nearest whole number using Math.floor(). This method also works with strings.

const sample = arr => arr[Math.floor(Math.random() * arr.length)];
 
sample([3, 7, 9, 11]); // 9
 

sampleSize

Gets n random elements at unique keys from array up to the size of array.

Shuffle the array using the Fisher-Yates algorithm. Use Array.slice() to get the first n elements. Omit the second argument, n to get only one element at random from the array.

const sampleSize = ([...arr], n = 1) => {
@@ -900,7 +900,7 @@ document.que
 anagramsCached('javascript'); // returns virtually instantly since it's now cached
 console.log(anagramsCached.cache); // The cached anagrams map
 

negate

Negates a predicate function.

Take a predicate function and apply the not operator (!) to it with its arguments.

const negate = func => (...args) => !func(...args);
-
[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 == 0)); // [ 1, 3, 5 ]
+
[1, 2, 3, 4, 5, 6].filter(negate(n => n % 2 === 0)); // [ 1, 3, 5 ]
 

once

Ensures a function is called only once.

Utilizing a closure, use a flag, called, and set it to true once the function is called for the first time, preventing it from being called again. In order to allow the function to have its this context changed (such as in an event listener), the function keyword must be used, and the supplied function must have the context applied. Allow the function to be supplied with an arbitrary number of arguments using the rest/spread (...) operator.

const once = fn => {
   let called = false;
   return function(...args) {
@@ -1057,7 +1057,7 @@ own individual rating by supplying it as the third argument.
 
isEven(3); // false
 

isPrime

Checks if the provided integer is a prime number.

Check numbers from 2 to the square root of the given number. Return false if any of them divides the given number, else return true, unless the number is less than 2.

const isPrime = num => {
   const boundary = Math.floor(Math.sqrt(num));
-  for (var i = 2; i <= boundary; i++) if (num % i == 0) return false;
+  for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
   return num >= 2;
 };
 
isPrime(11); // true
@@ -1102,7 +1102,7 @@ own individual rating by supplying it as the third argument.
   let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),
     sqroot = Math.floor(Math.sqrt(num)),
     numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);
-  numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y == x)));
+  numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));
   return arr;
 };
 
primes(10); // [2,3,5,7]