diff --git a/docs/archive.html b/docs/archive.html index 927e446bc..979772120 100644 --- a/docs/archive.html +++ b/docs/archive.html @@ -149,7 +149,7 @@ howManyTimes(100, -1); // Infinity

httpDelete

Makes a DELETE request to the passed URL.

Use XMLHttpRequest web api to make a delete request to the given url. Handle the onload event, by running the provided callback function. Handle the onerror event, by running the provided err function. Omit the third argument, err to log the request to the console's error stream by default.

const httpDelete = (url, callback, err = console.error) => {
   const request = new XMLHttpRequest();
-  request.open("DELETE", url, true);
+  request.open('DELETE', url, true);
   request.onload = () => callback(request);
   request.onerror = () => err(request);
   request.send();
@@ -158,12 +158,12 @@
   console.log(request.responseText);
 }); // 'Deletes a user from the database'
 

httpPut

Makes a PUT request to the passed URL.

Use XMLHttpRequest web api to make a put request to the given url. Set the value of an HTTP request header with setRequestHeader method. Handle the onload event, by running the provided callback function. Handle the onerror event, by running the provided err function. Omit the last argument, err to log the request to the console's error stream by default.

const httpPut = (url, data, callback, err = console.error) => {
-    const request = new XMLHttpRequest();
-    request.open("PUT", url, true);
-    request.setRequestHeader('Content-type','application/json; charset=utf-8');
-    request.onload = () => callback(request);
-    request.onerror = () => err(request);
-    request.send(data);
+  const request = new XMLHttpRequest();
+  request.open("PUT", url, true);
+  request.setRequestHeader('Content-type','application/json; charset=utf-8');
+  request.onload = () => callback(request);
+  request.onerror = () => err(request);
+  request.send(data);
 };
 
const password = "fooBaz";
 const data = JSON.stringify(password);
@@ -177,9 +177,15 @@
 
isArmstrongNumber(1634); // true
 isArmstrongNumber(56); // false
 

isSimilar

Determines if the pattern matches with str.

Use String.toLowerCase() to convert both strings to lowercase, then loop through str and determine if it contains all characters of pattern and in the correct order. Adapted from here.

const isSimilar = (pattern, str) =>
-	[...str].reduce(
-		(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex]  || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
-	) === pattern.length ? true : false;
+  [...str].reduce(
+    (matchIndex, char) =>
+      char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()
+        ? matchIndex + 1
+        : matchIndex,
+    0
+  ) === pattern.length
+    ? true
+    : false;
 
isSimilar('rt','Rohit'); // true
 isSimilar('tr','Rohit'); // false
 

JSONToDate

Converts a JSON object to a date.

Use Date(), to convert dates in JSON format to readable format (dd/mm/yyyy).

const JSONToDate = arr => {
@@ -188,21 +194,28 @@
 };
 
JSONToDate(/Date(1489525200000)/); // "14/3/2017"
 

levenshteinDistance

Calculates the Levenshtein distance between two strings.

Calculates the number of changes (substitutions, deletions or additions) required to convert string1 to string2. Can also be used to compare two strings as shown in the second example.

const levenshteinDistance = (string1, string2) => {
-    if(string1.length === 0) return string2.length;
-    if(string2.length === 0) return string1.length;
-    let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
-    matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
-    for(let i = 1; i <= string2.length; i++) {
-        for(let j = 1; j<=string1.length; j++) {
-            if(string2[i-1] === string1[j-1]) {
-                matrix[i][j] = matrix[i-1][j-1];
-            }
-            else{
-                matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1);
-            }
-        }
-    }
-    return matrix[string2.length][string1.length];
+  if (string1.length === 0) return string2.length;
+  if (string2.length === 0) return string1.length;
+  let matrix = Array(string2.length + 1)
+    .fill(0)
+    .map((x, i) => [i]);
+  matrix[0] = Array(string1.length + 1)
+    .fill(0)
+    .map((x, i) => i);
+  for (let i = 1; i <= string2.length; i++) {
+    for (let j = 1; j <= string1.length; j++) {
+      if (string2[i - 1] === string1[j - 1]) {
+        matrix[i][j] = matrix[i - 1][j - 1];
+      } else {
+        matrix[i][j] = Math.min(
+          matrix[i - 1][j - 1] + 1,
+          matrix[i][j - 1] + 1,
+          matrix[i - 1][j] + 1
+        );
+      }
+    }
+  }
+  return matrix[string2.length][string1.length];
 };
 
levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7
 const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));
@@ -217,7 +230,7 @@
       ];
 
quickSort([4, 1, 3, 2]); // [1,2,3,4]
 quickSort([4, 1, 3, 2], true); // [4,3,2,1]
-

removeVowels

Returns all the vowels in a str replaced by repl.

Use String.replace() with a regexp to replace all vowels in str. Omot repl to use a default value of ''.

const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi,repl);
+

removeVowels

Returns all the vowels in a str replaced by repl.

Use String.replace() with a regexp to replace all vowels in str. Omot repl to use a default value of ''.

const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl);
 
removeVowels("foobAr"); // "fbr"
 removeVowels("foobAr","*"); // "f**b*r"
 

solveRPN

Solves the given mathematical expression in reverse polish notation. Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- +,-,*,/,^,** (^&** are the exponential symbols and are same). This snippet does not supports any unary operators.

Use a dictionary, OPERATORS to specify each operator's matching mathematical operation. Use String.replace() with a regular expression to replace ^ with **, String.split() to tokenize the string and Array.filter() to remove empty tokens. Use Array.forEach() to parse each symbol, evaluate it as a numeric value or operator and solve the mathematical expression. Numeric values are converted to floating point numbers and pushed to a stack, while operators are evaluated using the OPERATORS dictionary and pop elements from the stack to apply operations.

const solveRPN = rpn => {