From 68fd827bb097881eac740fc85db9e1c1200df146 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 10:57:20 +0300 Subject: [PATCH 01/24] Remove collatz --- snippets_archive/collatz.md | 16 ---------------- test/collatz.test.js | 21 --------------------- 2 files changed, 37 deletions(-) delete mode 100644 snippets_archive/collatz.md delete mode 100644 test/collatz.test.js diff --git a/snippets_archive/collatz.md b/snippets_archive/collatz.md deleted file mode 100644 index a2b764087..000000000 --- a/snippets_archive/collatz.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -title: collatz -tags: math,beginner ---- - -Applies the Collatz algorithm. - -If `n` is even, return `n/2`. Otherwise, return `3n+1`. - -```js -const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1); -``` - -```js -collatz(8); // 4 -``` \ No newline at end of file diff --git a/test/collatz.test.js b/test/collatz.test.js deleted file mode 100644 index 29ea7e513..000000000 --- a/test/collatz.test.js +++ /dev/null @@ -1,21 +0,0 @@ -const {collatz} = require('./_30s.js'); - -test('collatz is a Function', () => { - expect(collatz).toBeInstanceOf(Function); -}); -test('When n is even, divide by 2', () => { - expect(collatz(8)).toBe(4); -}); -test('When n is odd, times by 3 and add 1', () => { - expect(collatz(9)).toBe(28); -}); -test('Eventually reaches 1', () => { - let n = 9; - while (true) { - if (n === 1) { - expect(n).toBe(1); - break; - } - n = collatz(n); - } -}); From f0d8ca0b8c4be869696b69a811e6ff87f46f5e93 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 10:57:47 +0300 Subject: [PATCH 02/24] Remove heronArea --- snippets_archive/heronArea.md | 21 --------------------- test/heronArea.test.js | 8 -------- 2 files changed, 29 deletions(-) delete mode 100644 snippets_archive/heronArea.md delete mode 100644 test/heronArea.test.js diff --git a/snippets_archive/heronArea.md b/snippets_archive/heronArea.md deleted file mode 100644 index 1be742986..000000000 --- a/snippets_archive/heronArea.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -title: heronArea -tags: math,beginner ---- - -Returns the area of a triangle using only the 3 side lengths, Heron's formula. Assumes that the sides define a valid triangle. Does NOT assume it is a right triangle. - -More information on what Heron's formula is and why it works available here: https://en.wikipedia.org/wiki/Heron%27s_formula. - -Uses `Math.sqrt()` to find the square root of a value. - -```js -const heronArea = (side_a, side_b, side_c) => { - const p = (side_a + side_b + side_c) / 2 - return Math.sqrt(p * (p-side_a) * (p-side_b) * (p-side_c)) - }; -``` - -```js -heronArea(3, 4, 5); // 6 -``` \ No newline at end of file diff --git a/test/heronArea.test.js b/test/heronArea.test.js deleted file mode 100644 index 70c0173c2..000000000 --- a/test/heronArea.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {heronArea} = require('./_30s.js'); - -test('heronArea is a Function', () => { - expect(heronArea).toBeInstanceOf(Function); -}); -test('howManyTimes returns the correct result', () => { - expect(heronArea(3, 4, 5)).toBe(6); -}); From d826a5ee6f56d490398b69490627ab8bc74b1042 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 10:58:38 +0300 Subject: [PATCH 03/24] Remove countVowels --- snippets_archive/countVowels.md | 17 ----------------- test/countVowels.test.js | 11 ----------- 2 files changed, 28 deletions(-) delete mode 100644 snippets_archive/countVowels.md delete mode 100644 test/countVowels.test.js diff --git a/snippets_archive/countVowels.md b/snippets_archive/countVowels.md deleted file mode 100644 index 4111a147a..000000000 --- a/snippets_archive/countVowels.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: countVowels -tags: string,beginner ---- - -Returns `number` of vowels in provided string. - -Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a `string`. - -```js -const countVowels = str => (str.match(/[aeiou]/gi) || []).length; -``` - -```js -countVowels('foobar'); // 3 -countVowels('gym'); // 0 -``` \ No newline at end of file diff --git a/test/countVowels.test.js b/test/countVowels.test.js deleted file mode 100644 index fb6178e69..000000000 --- a/test/countVowels.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const {countVowels} = require('./_30s.js'); - -test('countVowels is a Function', () => { - expect(countVowels).toBeInstanceOf(Function); -}); -test('countVowels returns the correct count', () => { - expect(countVowels('foobar')).toBe(3); -}); -test('countVowels returns the correct count', () => { - expect(countVowels('ggg')).toBe(0); -}); From 31eb0269a8921b16f927b5a2b3aa42220d716914 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:00:06 +0300 Subject: [PATCH 04/24] Migrate celsius/fahrenheit conversions --- {snippets_archive => snippets}/celsiusToFahrenheit.md | 4 ++-- {snippets_archive => snippets}/fahrenheitToCelsius.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename {snippets_archive => snippets}/celsiusToFahrenheit.md (81%) rename {snippets_archive => snippets}/fahrenheitToCelsius.md (81%) diff --git a/snippets_archive/celsiusToFahrenheit.md b/snippets/celsiusToFahrenheit.md similarity index 81% rename from snippets_archive/celsiusToFahrenheit.md rename to snippets/celsiusToFahrenheit.md index 486f1f2e4..4ca37e450 100644 --- a/snippets_archive/celsiusToFahrenheit.md +++ b/snippets/celsiusToFahrenheit.md @@ -3,7 +3,7 @@ title: celsiusToFahrenheit tags: math,beginner --- -Celsius to Fahrenheit temperature conversion. +Converts Celsius to Fahrenheit. Follows the conversion formula `F = 1.8C + 32`. @@ -13,4 +13,4 @@ const celsiusToFahrenheit = degrees => 1.8 * degrees + 32; ```js celsiusToFahrenheit(33) // 91.4 -``` \ No newline at end of file +``` diff --git a/snippets_archive/fahrenheitToCelsius.md b/snippets/fahrenheitToCelsius.md similarity index 81% rename from snippets_archive/fahrenheitToCelsius.md rename to snippets/fahrenheitToCelsius.md index 0b2d9d186..7b23eb337 100644 --- a/snippets_archive/fahrenheitToCelsius.md +++ b/snippets/fahrenheitToCelsius.md @@ -3,7 +3,7 @@ title: fahrenheitToCelsius tags: math,beginner --- -Fahrenheit to Celsius temperature conversion. +Converts Fahrenheit to Celsius. Follows the conversion formula `C = (F - 32) * 5/9`. @@ -13,4 +13,4 @@ const fahrenheitToCelsius = degrees => (degrees - 32) * 5/9; ```js fahrenheitToCelsius(32); // 0 -``` \ No newline at end of file +``` From 410f4443caf672758aef879fe7c6731c8da4a018 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:02:33 +0300 Subject: [PATCH 05/24] Remove fibonacciCountUntilNum --- snippets_archive/fibonacciCountUntilNum.md | 17 ----------------- test/fibonacciCountUntilNum.test.js | 8 -------- 2 files changed, 25 deletions(-) delete mode 100644 snippets_archive/fibonacciCountUntilNum.md delete mode 100644 test/fibonacciCountUntilNum.test.js diff --git a/snippets_archive/fibonacciCountUntilNum.md b/snippets_archive/fibonacciCountUntilNum.md deleted file mode 100644 index e3cc779b4..000000000 --- a/snippets_archive/fibonacciCountUntilNum.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: fibonacciCountUntilNum -tags: math,beginner ---- - -Returns the number of fibonnacci numbers up to `num`(`0` and `num` inclusive). - -Use a mathematical formula to calculate the number of fibonacci numbers until `num`. - -```js -const fibonacciCountUntilNum = num => - Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); -``` - -```js -fibonacciCountUntilNum(10); // 7 -``` \ No newline at end of file diff --git a/test/fibonacciCountUntilNum.test.js b/test/fibonacciCountUntilNum.test.js deleted file mode 100644 index 83a053f0b..000000000 --- a/test/fibonacciCountUntilNum.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {fibonacciCountUntilNum} = require('./_30s.js'); - -test('fibonacciCountUntilNum is a Function', () => { - expect(fibonacciCountUntilNum).toBeInstanceOf(Function); -}); -test('Returns the correct number', () => { - expect(fibonacciCountUntilNum(10)).toBe(7); -}); From 80fda6d3fc29cc608d1c136d27b340923f8915a5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:02:44 +0300 Subject: [PATCH 06/24] Remove fibonacciUntilNum --- snippets_archive/fibonacciUntilNum.md | 24 ------------------------ test/fibonacciUntilNum.test.js | 8 -------- 2 files changed, 32 deletions(-) delete mode 100644 snippets_archive/fibonacciUntilNum.md delete mode 100644 test/fibonacciUntilNum.test.js diff --git a/snippets_archive/fibonacciUntilNum.md b/snippets_archive/fibonacciUntilNum.md deleted file mode 100644 index f4f04ef90..000000000 --- a/snippets_archive/fibonacciUntilNum.md +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: fibonacciUntilNum -tags: math,intermediate ---- - -Generates an array, containing the Fibonacci sequence, up until the nth term. - -Create an empty array of the specific length, initializing the first two values (`0` and `1`). -Use `Array.prototype.reduce()` to add values into the array, using the sum of the last two values, except for the first two. -Uses a mathematical formula to calculate the length of the array required. - -```js -const fibonacciUntilNum = num => { - let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); - return Array.from({ length: n }).reduce( - (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), - [] - ); -}; -``` - -```js -fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ] -``` \ No newline at end of file diff --git a/test/fibonacciUntilNum.test.js b/test/fibonacciUntilNum.test.js deleted file mode 100644 index 53f0b2fd0..000000000 --- a/test/fibonacciUntilNum.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {fibonacciUntilNum} = require('./_30s.js'); - -test('fibonacciUntilNum is a Function', () => { - expect(fibonacciUntilNum).toBeInstanceOf(Function); -}); -test('Returns the correct sequence', () => { - expect(fibonacciUntilNum(10)).toEqual([0, 1, 1, 2, 3, 5, 8]); -}); From e099e8246edd211f3b8c8ac28fa06c73be155466 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:04:51 +0300 Subject: [PATCH 07/24] Remove mphToKmph --- snippets_archive/mphToKmph.md | 17 ----------------- test/mphToKmph.test.js | 8 -------- 2 files changed, 25 deletions(-) delete mode 100644 snippets_archive/mphToKmph.md delete mode 100644 test/mphToKmph.test.js diff --git a/snippets_archive/mphToKmph.md b/snippets_archive/mphToKmph.md deleted file mode 100644 index 6d0eb6e5f..000000000 --- a/snippets_archive/mphToKmph.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: mphToKmph -tags: math,beginner ---- - -Convert miles/hour to kilometers/hour. - -Multiply the constant of proportionality with the argument. - -```js -const mphToKmph = (mph) => 1.6093440006146922 * mph; -``` - -```js -mphToKmph(10); // 16.09344000614692 -mphToKmph(85.9); // 138.24264965280207 -``` \ No newline at end of file diff --git a/test/mphToKmph.test.js b/test/mphToKmph.test.js deleted file mode 100644 index 93ab9fa43..000000000 --- a/test/mphToKmph.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {mphToKmph} = require('./_30s.js'); - -test('mphToKmph is a Function', () => { - expect(mphToKmph).toBeInstanceOf(Function); -}); -test('Returns kph from mph.', () => { - expect(mphToKmph(10)).toBe(16.09344000614692); -}); From d96e0f02413a9ad0bbad47909f0b7b5ad32c80b0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:06:35 +0300 Subject: [PATCH 08/24] Remove howManyTimes --- snippets_archive/howManyTimes.md | 32 -------------------------------- test/howManyTimes.test.js | 17 ----------------- 2 files changed, 49 deletions(-) delete mode 100644 snippets_archive/howManyTimes.md delete mode 100644 test/howManyTimes.test.js diff --git a/snippets_archive/howManyTimes.md b/snippets_archive/howManyTimes.md deleted file mode 100644 index e0569e0bf..000000000 --- a/snippets_archive/howManyTimes.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: howManyTimes -tags: math,beginner ---- - -Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer. -Works for both negative and positive integers. - -If `divisor` is `-1` or `1` return `Infinity`. -If `divisor` is `-0` or `0` return `0`. -Otherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer. -Return the number of times the loop was executed, `i`. - -```js -const howManyTimes = (num, divisor) => { - if (divisor === 1 || divisor === -1) return Infinity; - if (divisor === 0) return 0; - let i = 0; - while (Number.isInteger(num / divisor)) { - i++; - num = num / divisor; - } - return i; -}; -``` - -```js -howManyTimes(100, 2); // 2 -howManyTimes(100, 2.5); // 2 -howManyTimes(100, 0); // 0 -howManyTimes(100, -1); // Infinity -``` \ No newline at end of file diff --git a/test/howManyTimes.test.js b/test/howManyTimes.test.js deleted file mode 100644 index 7aa6f4301..000000000 --- a/test/howManyTimes.test.js +++ /dev/null @@ -1,17 +0,0 @@ -const {howManyTimes} = require('./_30s.js'); - -test('howManyTimes is a Function', () => { - expect(howManyTimes).toBeInstanceOf(Function); -}); -test('howManyTimes returns the correct result', () => { - expect(howManyTimes(100, 2)).toBe(2); -}); -test('howManyTimes returns the correct result', () => { - expect(howManyTimes(100, 2.5)).toBe(2); -}); -test('howManyTimes returns the correct result', () => { - expect(howManyTimes(100, 0)).toBe(0); -}); -test('howManyTimes returns the correct result', () => { - expect(howManyTimes(100, -1)).toBe(Infinity); -}); From 90a71bf9d7e22dbb286c4638551335a642451562 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:07:55 +0300 Subject: [PATCH 09/24] Remove isArmstrongNumber --- snippets_archive/isArmstrongNumber.md | 20 -------------------- test/isArmstrongNumber.test.js | 11 ----------- 2 files changed, 31 deletions(-) delete mode 100644 snippets_archive/isArmstrongNumber.md delete mode 100644 test/isArmstrongNumber.test.js diff --git a/snippets_archive/isArmstrongNumber.md b/snippets_archive/isArmstrongNumber.md deleted file mode 100644 index 96c3b205d..000000000 --- a/snippets_archive/isArmstrongNumber.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: isArmstrongNumber -tags: math,beginner ---- - -Checks if the given number is an Armstrong number or not. - -Convert the given number into an array of digits. Use the exponent operator (`**`) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. - -```js -const isArmstrongNumber = digits => - (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)( - (digits + '').split('') - ); -``` - -```js -isArmstrongNumber(1634); // true -isArmstrongNumber(56); // false -``` \ No newline at end of file diff --git a/test/isArmstrongNumber.test.js b/test/isArmstrongNumber.test.js deleted file mode 100644 index 7edc26daf..000000000 --- a/test/isArmstrongNumber.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const {isArmstrongNumber} = require('./_30s.js'); - -test('isArmstrongNumber is a Function', () => { - expect(isArmstrongNumber).toBeInstanceOf(Function); -}); -test('isArmstrongNumber returns true', () => { - expect(isArmstrongNumber(1634)).toBeTruthy(); -}); -test('isArmstrongNumber returns false', () => { - expect(isArmstrongNumber(56)).toBeFalsy(); -}); From 8a45b51dc3817cb11720d06e64690713ec7c7d10 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:08:26 +0300 Subject: [PATCH 10/24] Remove isSimilar --- snippets_archive/isSimilar.md | 25 ------------------------- test/isSimilar.test.js | 11 ----------- 2 files changed, 36 deletions(-) delete mode 100644 snippets_archive/isSimilar.md delete mode 100644 test/isSimilar.test.js diff --git a/snippets_archive/isSimilar.md b/snippets_archive/isSimilar.md deleted file mode 100644 index 785ab130e..000000000 --- a/snippets_archive/isSimilar.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -title: isSimilar -tags: string,intermediate ---- - -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](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18). - -```js -const isSimilar = (pattern, str) => - [...str].reduce( - (matchIndex, char) => - char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() - ? matchIndex + 1 - : matchIndex, - 0 - ) === pattern.length; -``` - -```js -isSimilar('rt','Rohit'); // true -isSimilar('tr','Rohit'); // false -``` \ No newline at end of file diff --git a/test/isSimilar.test.js b/test/isSimilar.test.js deleted file mode 100644 index 22f069283..000000000 --- a/test/isSimilar.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const {isSimilar} = require('./_30s.js'); - -test('isSimilar is a Function', () => { - expect(isSimilar).toBeInstanceOf(Function); -}); -test('isSimilar returns true', () => { - expect(isSimilar('rt', 'Rohit')).toBeTruthy(); -}); -test('isSimilar returns false', () => { - expect(isSimilar('tr', 'Rohit')).toBeFalsy(); -}); From 85476aba278928c1855575916eb11257c4cfe84e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:09:02 +0300 Subject: [PATCH 11/24] Remove JSONToDate --- snippets_archive/JSONToDate.md | 19 ------------------- test/JSONToDate.test.js | 9 --------- 2 files changed, 28 deletions(-) delete mode 100644 snippets_archive/JSONToDate.md delete mode 100644 test/JSONToDate.test.js diff --git a/snippets_archive/JSONToDate.md b/snippets_archive/JSONToDate.md deleted file mode 100644 index ae88cb2bc..000000000 --- a/snippets_archive/JSONToDate.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -title: JSONToDate -tags: object,date,beginner ---- - -Converts a JSON object to a date. - -Use `Date()`, to convert dates in JSON format to readable format (`dd/mm/yyyy`). - -```js -const JSONToDate = arr => { - const dt = new Date(parseInt(arr.toString().substr(6))); - return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`; -}; -``` - -```js -JSONToDate(/Date(1489525200000)/); // "14/3/2017" -``` \ No newline at end of file diff --git a/test/JSONToDate.test.js b/test/JSONToDate.test.js deleted file mode 100644 index 97888c08f..000000000 --- a/test/JSONToDate.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const {JSONToDate} = require('./_30s.js'); - -test('JSONToDate is a Function', () => { - expect(JSONToDate).toBeInstanceOf(Function); -}); -test('JSONToDate returns the correct date string', () => { - var reg = new RegExp(`Date(${Date.parse('March 14,2017')})`); - expect(JSONToDate(reg)).toBe('14/3/2017'); -}); From ca0de2deba1906125e567b7975bf0108991eb342 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:09:37 +0300 Subject: [PATCH 12/24] Remove squareSum --- snippets_archive/squareSum.md | 16 ---------------- test/squareSum.test.js | 16 ---------------- 2 files changed, 32 deletions(-) delete mode 100644 snippets_archive/squareSum.md delete mode 100644 test/squareSum.test.js diff --git a/snippets_archive/squareSum.md b/snippets_archive/squareSum.md deleted file mode 100644 index af17a6c2e..000000000 --- a/snippets_archive/squareSum.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -title: squareSum -tags: math,beginner ---- - -Squares each number in an array and then sums the results together. - -Use `Array.prototype.reduce()` in combination with `Math.pow()` to iterate over numbers and sum their squares into an accumulator. - -```js -const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0); -``` - -```js -squareSum(1, 2, 2); // 9 -``` \ No newline at end of file diff --git a/test/squareSum.test.js b/test/squareSum.test.js deleted file mode 100644 index 8219f52f4..000000000 --- a/test/squareSum.test.js +++ /dev/null @@ -1,16 +0,0 @@ -const {squareSum} = require('./_30s.js'); - -test('squareSum is a Function', () => { - expect(squareSum).toBeInstanceOf(Function); -}); -test('squareSum returns the proper result', () => { - expect(squareSum(2, 3, 4)).toBe(29); -}); - -test('works with negative numbers', () => { - expect(squareSum(-2, 3, -4)).toBe(29); -}); - -test('NaN when parameters have text', () => { - expect(squareSum(-2, 3, -4, 'text')).toBe(NaN); -}); From 589e78962a7ebc0588ad609d399ed813a0dd33bc Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:10:13 +0300 Subject: [PATCH 13/24] Migrate objectToEntries --- {snippets_archive => snippets}/objectToEntries.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {snippets_archive => snippets}/objectToEntries.md (100%) diff --git a/snippets_archive/objectToEntries.md b/snippets/objectToEntries.md similarity index 100% rename from snippets_archive/objectToEntries.md rename to snippets/objectToEntries.md From ec38b481eb045e79c3c68f3b3ebe8da8f4f3291e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:10:44 +0300 Subject: [PATCH 14/24] Remove factors --- snippets_archive/factors.md | 45 ------------------------------------- test/factors.test.js | 17 -------------- 2 files changed, 62 deletions(-) delete mode 100644 snippets_archive/factors.md delete mode 100644 test/factors.test.js diff --git a/snippets_archive/factors.md b/snippets_archive/factors.md deleted file mode 100644 index 0605a6ea1..000000000 --- a/snippets_archive/factors.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -title: factors -tags: math,intermediate ---- - -Returns the array of factors of the given `num`. -If the second argument is set to `true` returns only the prime factors of `num`. -If `num` is `1` or `0` returns an empty array. -If `num` is less than `0` returns all the factors of `-int` together with their additive inverses. - -Use `Array.from()`, `Array.prototype.map()` and `Array.prototype.filter()` to find all the factors of `num`. -If given `num` is negative, use `Array.prototype.reduce()` to add the additive inverses to the array. -Return all results if `primes` is `false`, else determine and return only the prime factors using `isPrime` and `Array.prototype.filter()`. -Omit the second argument, `primes`, to return prime and non-prime factors by default. - -**Note**:- _Negative numbers are not considered prime._ - -```js -const factors = (num, primes = false) => { - const isPrime = num => { - const boundary = Math.floor(Math.sqrt(num)); - for (var i = 2; i <= boundary; i++) if (num % i === 0) return false; - return num >= 2; - }; - const isNeg = num < 0; - num = isNeg ? -num : num; - let array = Array.from({ length: num - 1 }) - .map((val, i) => (num % (i + 2) === 0 ? i + 2 : false)) - .filter(val => val); - if (isNeg) - array = array.reduce((acc, val) => { - acc.push(val); - acc.push(-val); - return acc; - }, []); - return primes ? array.filter(isPrime) : array; -}; -``` - -```js -factors(12); // [2,3,4,6,12] -factors(12, true); // [2,3] -factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12] -factors(-12, true); // [2,3] -``` \ No newline at end of file diff --git a/test/factors.test.js b/test/factors.test.js deleted file mode 100644 index 8fc7f0f5c..000000000 --- a/test/factors.test.js +++ /dev/null @@ -1,17 +0,0 @@ -const {factors} = require('./_30s.js'); - -test('factors is a Function', () => { - expect(factors).toBeInstanceOf(Function); -}); -test('factors returns the correct array', () => { - expect(factors(12)).toEqual([2, 3, 4, 6, 12]); -}); -test('factors returns the correct array of primes', () => { - expect(factors(12, true)).toEqual([2, 3]); -}); -test('factors returns the correct array for negatives', () => { - expect(factors(-12)).toEqual([2, -2, 3, -3, 4, -4, 6, -6, 12, -12]); -}); -test('factors returns the correct array of primes for negatives', () => { - expect(factors(-12, true)).toEqual([2, 3]); -}); From 66421c9aaa095b3063dd74e1a144dfdb84bc2044 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:11:48 +0300 Subject: [PATCH 15/24] Remove kmphToMph --- snippets_archive/kmphToMph.md | 17 ----------------- test/kmphToMph.test.js | 8 -------- 2 files changed, 25 deletions(-) delete mode 100644 snippets_archive/kmphToMph.md delete mode 100644 test/kmphToMph.test.js diff --git a/snippets_archive/kmphToMph.md b/snippets_archive/kmphToMph.md deleted file mode 100644 index 36685e1f7..000000000 --- a/snippets_archive/kmphToMph.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: kmphToMph -tags: math,beginner ---- - -Convert kilometers/hour to miles/hour. - -Multiply the constant of proportionality with the argument. - -```js -const kmphToMph = (kmph) => 0.621371192 * kmph; -``` - -```js -kmphToMph(10); // 16.09344000614692 -kmphToMph(345.4); // 138.24264965280207 -``` \ No newline at end of file diff --git a/test/kmphToMph.test.js b/test/kmphToMph.test.js deleted file mode 100644 index b926964b8..000000000 --- a/test/kmphToMph.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {kmphToMph} = require('./_30s.js'); - -test('kmphToMph is a Function', () => { - expect(kmphToMph).toBeInstanceOf(Function); -}); -test('Returns mph from kph.', () => { - expect(kmphToMph(10)).toBe(6.21371192); -}); From 81a78b2ba9e3cc017fe246f712f3d2a8e85a15a8 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:12:17 +0300 Subject: [PATCH 16/24] Remove pipeLog --- snippets_archive/pipeLog.md | 16 ---------------- test/pipeLog.test.js | 8 -------- 2 files changed, 24 deletions(-) delete mode 100644 snippets_archive/pipeLog.md delete mode 100644 test/pipeLog.test.js diff --git a/snippets_archive/pipeLog.md b/snippets_archive/pipeLog.md deleted file mode 100644 index 5623b465f..000000000 --- a/snippets_archive/pipeLog.md +++ /dev/null @@ -1,16 +0,0 @@ ---- -title: pipeLog -tags: utility,beginner ---- - -Logs a value and returns it. - -Use `console.log` to log the supplied value, combined with the `||` operator to return it. - -```js -const pipeLog = data => console.log(data) || data; -``` - -```js -pipeLog(1); // logs `1` and returns `1` -``` \ No newline at end of file diff --git a/test/pipeLog.test.js b/test/pipeLog.test.js deleted file mode 100644 index 0244ecba2..000000000 --- a/test/pipeLog.test.js +++ /dev/null @@ -1,8 +0,0 @@ -const {pipeLog} = require('./_30s.js'); - -test('pipeLog is a Function', () => { - expect(pipeLog).toBeInstanceOf(Function); -}); -test('pipeLog returns the given value', () => { - expect(pipeLog('hi')).toBe('hi'); -}); From 7380db04b306dc0ae558d9f4a8ac766e2ab2c137 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:12:44 +0300 Subject: [PATCH 17/24] Remove removeVowels --- snippets_archive/removeVowels.md | 18 ------------------ test/removeVowels.test.js | 11 ----------- 2 files changed, 29 deletions(-) delete mode 100644 snippets_archive/removeVowels.md delete mode 100644 test/removeVowels.test.js diff --git a/snippets_archive/removeVowels.md b/snippets_archive/removeVowels.md deleted file mode 100644 index f7fb45896..000000000 --- a/snippets_archive/removeVowels.md +++ /dev/null @@ -1,18 +0,0 @@ ---- -title: removeVowels -tags: string,beginner ---- - -Returns all the vowels in a `str` replaced by `repl`. - -Use `String.prototype.replace()` with a regexp to replace all vowels in `str`. -Omot `repl` to use a default value of `''`. - -```js -const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl); -``` - -```js -removeVowels("foobAr"); // "fbr" -removeVowels("foobAr","*"); // "f**b*r" -``` \ No newline at end of file diff --git a/test/removeVowels.test.js b/test/removeVowels.test.js deleted file mode 100644 index fa77158b9..000000000 --- a/test/removeVowels.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const {removeVowels} = require('./_30s.js'); - -test('removeVowels is a Function', () => { - expect(removeVowels).toBeInstanceOf(Function); -}); -test('Removes vowels.', () => { - expect(removeVowels('foobAr')).toBe('fbr'); -}); -test('Replaces vowels.', () => { - expect(removeVowels('foobAr', '*')).toBe('f**b*r'); -}); From 5de07a4337f496eb6f8a51a001e8bd779efad879 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:13:22 +0300 Subject: [PATCH 18/24] Remove solveRPN --- snippets_archive/solveRPN.md | 48 ------------------------------------ test/solveRPN.test.js | 11 --------- 2 files changed, 59 deletions(-) delete mode 100644 snippets_archive/solveRPN.md delete mode 100644 test/solveRPN.test.js diff --git a/snippets_archive/solveRPN.md b/snippets_archive/solveRPN.md deleted file mode 100644 index 2bca5d81b..000000000 --- a/snippets_archive/solveRPN.md +++ /dev/null @@ -1,48 +0,0 @@ ---- -title: solveRPN -tags: algorithm,advanced ---- - -Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/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.prototype.replace()` with a regular expression to replace `^` with `**`, `String.prototype.split()` to tokenize the string and `Array.prototype.filter()` to remove empty tokens. -Use `Array.prototype.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. - -```js -const solveRPN = rpn => { - const OPERATORS = { - '*': (a, b) => a * b, - '+': (a, b) => a + b, - '-': (a, b) => a - b, - '/': (a, b) => a / b, - '**': (a, b) => a ** b - }; - const [stack, solve] = [ - [], - rpn - .replace(/\^/g, '**') - .split(/\s+/g) - .filter(el => !/\s+/.test(el) && el !== '') - ]; - solve.forEach(symbol => { - if (!isNaN(parseFloat(symbol)) && isFinite(symbol)) { - stack.push(symbol); - } else if (Object.keys(OPERATORS).includes(symbol)) { - const [a, b] = [stack.pop(), stack.pop()]; - stack.push(OPERATORS[symbol](parseFloat(b), parseFloat(a))); - } else { - throw `${symbol} is not a recognized symbol`; - } - }); - if (stack.length === 1) return stack.pop(); - else throw `${rpn} is not a proper RPN. Please check it and try again`; -}; -``` - -```js -solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5 -solveRPN('2 3 ^'); // 8 -``` \ No newline at end of file diff --git a/test/solveRPN.test.js b/test/solveRPN.test.js deleted file mode 100644 index 8fcbd380e..000000000 --- a/test/solveRPN.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const {solveRPN} = require('./_30s.js'); - -test('solveRPN is a Function', () => { - expect(solveRPN).toBeInstanceOf(Function); -}); -test('solveRPN returns the correct result', () => { - expect(solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -')).toBe(5); -}); -test('solveRPN returns the correct result', () => { - expect(solveRPN('2 3 ^')).toBe(8); -}); From 638e09d730fa148b48a627b03bb13563dd813fe0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:21:33 +0300 Subject: [PATCH 19/24] Migrate httpDelete, httpPut --- {snippets_archive => snippets}/httpDelete.md | 10 +++++---- {snippets_archive => snippets}/httpPut.md | 22 +++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) rename {snippets_archive => snippets}/httpDelete.md (83%) rename {snippets_archive => snippets}/httpPut.md (75%) diff --git a/snippets_archive/httpDelete.md b/snippets/httpDelete.md similarity index 83% rename from snippets_archive/httpDelete.md rename to snippets/httpDelete.md index 5552a25ae..7fafa8093 100644 --- a/snippets_archive/httpDelete.md +++ b/snippets/httpDelete.md @@ -1,6 +1,6 @@ --- title: httpDelete -tags: browser,intermediate +tags: utility,url,browser,intermediate --- Makes a `DELETE` request to the passed URL. @@ -21,7 +21,9 @@ const httpDelete = (url, callback, err = console.error) => { ``` ```js -httpDelete('https://website.com/users/123', request => { +httpDelete('https://jsonplaceholder.typicode.com/posts/1', request => { console.log(request.responseText); -}); // 'Deletes a user from the database' -``` \ No newline at end of file +}); /* +Logs: {} +*/ +``` diff --git a/snippets_archive/httpPut.md b/snippets/httpPut.md similarity index 75% rename from snippets_archive/httpPut.md rename to snippets/httpPut.md index 14488f79d..d7bf03172 100644 --- a/snippets_archive/httpPut.md +++ b/snippets/httpPut.md @@ -1,6 +1,6 @@ --- title: httpPut -tags: browser,intermediate +tags: utility,url,browser,intermediate --- Makes a `PUT` request to the passed URL. @@ -24,8 +24,20 @@ const httpPut = (url, data, callback, err = console.error) => { ```js const password = "fooBaz"; -const data = JSON.stringify(password); -httpPut('https://website.com/users/123', data, request => { +const data = JSON.stringify({ + id: 1, + title: 'foo', + body: 'bar', + userId: 1 +}); +httpPut('https://jsonplaceholder.typicode.com/posts/1', data, request => { console.log(request.responseText); -}); // 'Updates a user's password in database' -``` \ No newline at end of file +}); /* +Logs: { + id: 1, + title: 'foo', + body: 'bar', + userId: 1 +} +*/ +``` From 007e9394e621bf89da75e8f55760fb9db3ecc28e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:32:23 +0300 Subject: [PATCH 20/24] Remove cleanObj --- snippets_archive/cleanObj.md | 27 --------------------------- test/cleanObj.test.js | 9 --------- 2 files changed, 36 deletions(-) delete mode 100644 snippets_archive/cleanObj.md delete mode 100644 test/cleanObj.test.js diff --git a/snippets_archive/cleanObj.md b/snippets_archive/cleanObj.md deleted file mode 100644 index 40956a51a..000000000 --- a/snippets_archive/cleanObj.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: cleanObj -tags: object,beginner ---- - -Removes any properties except the ones specified from a JSON object. - -Use `Object.keys()` method to loop over given JSON object and deleting keys that are not included in given array. -If you pass a special key,`childIndicator`, it will search deeply apply the function to inner objects, too. - -```js -const cleanObj = (obj, keysToKeep = [], childIndicator) => { - Object.keys(obj).forEach(key => { - if (key === childIndicator) { - cleanObj(obj[key], keysToKeep, childIndicator); - } else if (!keysToKeep.includes(key)) { - delete obj[key]; - } - }); - return obj; -}; -``` - -```js -const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } }; -cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}} -``` \ No newline at end of file diff --git a/test/cleanObj.test.js b/test/cleanObj.test.js deleted file mode 100644 index 4cc144c5a..000000000 --- a/test/cleanObj.test.js +++ /dev/null @@ -1,9 +0,0 @@ -const {cleanObj} = require('./_30s.js'); - -test('cleanObj is a Function', () => { - expect(cleanObj).toBeInstanceOf(Function); -}); -const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } }; -test('Removes any properties except the ones specified from a JSON object', () => { - expect(cleanObj(testObj, ['a'], 'children')).toEqual({ a: 1, children: { a: 1 } }); -}); From 321970f27f7bff043f46acf28fedd6429b7873cb Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:32:33 +0300 Subject: [PATCH 21/24] Remove speechSynthesis --- snippets_archive/speechSynthesis.md | 23 ----------------------- test/speechSynthesis.test.js | 5 ----- 2 files changed, 28 deletions(-) delete mode 100644 snippets_archive/speechSynthesis.md delete mode 100644 test/speechSynthesis.test.js diff --git a/snippets_archive/speechSynthesis.md b/snippets_archive/speechSynthesis.md deleted file mode 100644 index 18f2229f8..000000000 --- a/snippets_archive/speechSynthesis.md +++ /dev/null @@ -1,23 +0,0 @@ ---- -title: speechSynthesis -tags: browser,intermediate ---- - -Performs speech synthesis (experimental). - -Use `SpeechSynthesisUtterance.voice` and `window.speechSynthesis.getVoices()` to convert a message to speech. -Use `window.speechSynthesis.speak()` to play the message. - -Learn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance). - -```js -const speechSynthesis = message => { - const msg = new SpeechSynthesisUtterance(message); - msg.voice = window.speechSynthesis.getVoices()[0]; - window.speechSynthesis.speak(msg); -}; -``` - -```js -speechSynthesis('Hello, World'); // // plays the message -``` \ No newline at end of file diff --git a/test/speechSynthesis.test.js b/test/speechSynthesis.test.js deleted file mode 100644 index f5a6c29e1..000000000 --- a/test/speechSynthesis.test.js +++ /dev/null @@ -1,5 +0,0 @@ -const {speechSynthesis} = require('./_30s.js'); - -test('speechSynthesis is a Function', () => { - expect(speechSynthesis).toBeInstanceOf(Function); -}); From 5de3a6526dd38ff1f643e1399eabe13ec59e99f5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:32:40 +0300 Subject: [PATCH 22/24] Remove quickSort --- snippets_archive/quickSort.md | 26 ---------------------- test/quickSort.test.js | 42 ----------------------------------- 2 files changed, 68 deletions(-) delete mode 100644 snippets_archive/quickSort.md delete mode 100644 test/quickSort.test.js diff --git a/snippets_archive/quickSort.md b/snippets_archive/quickSort.md deleted file mode 100644 index 6b2a58e1a..000000000 --- a/snippets_archive/quickSort.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -title: quickSort -tags: algorithm,recursion,beginner ---- - -QuickSort an Array (ascending sort by default). - -Use recursion. -Use `Array.prototype.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. -If the parameter `desc` is truthy, return array sorts in descending order. - -```js -const quickSort = ([n, ...nums], desc) => - isNaN(n) - ? [] - : [ - ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc), - n, - ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc) - ]; -``` - -```js -quickSort([4, 1, 3, 2]); // [1,2,3,4] -quickSort([4, 1, 3, 2], true); // [4,3,2,1] -``` \ No newline at end of file diff --git a/test/quickSort.test.js b/test/quickSort.test.js deleted file mode 100644 index 088a17eec..000000000 --- a/test/quickSort.test.js +++ /dev/null @@ -1,42 +0,0 @@ -const {quickSort} = require('./_30s.js'); - -test('quickSort is a Function', () => { - expect(quickSort).toBeInstanceOf(Function); -}); -test('quickSort([5, 6, 4, 3, 1, 2]) returns [1, 2, 3, 4, 5, 6]', () => { - expect(quickSort([5, 6, 4, 3, 1, 2])).toEqual([1, 2, 3, 4, 5, 6]); -}); -test('quickSort([-1, 0, -2]) returns [-2, -1, 0]', () => { - expect(quickSort([-1, 0, -2])).toEqual([-2, -1, 0]); -}); -test('quickSort() throws an error', () => { - expect(() => { - quickSort(); - }).toThrow(); -}); -test('quickSort(123) throws an error', () => { - expect(() => { - quickSort(123); - }).toThrow(); -}); -test('quickSort({ 234: string}) throws an error', () => { - expect(() => { - quickSort({ 234: string }); - }).toThrow(); -}); -test('quickSort(null) throws an error', () => { - expect(() => { - quickSort(null); - }).toThrow(); -}); -test('quickSort(undefined) throws an error', () => { - expect(() => { - quickSort(undefined); - }).toThrow(); -}); -let start = new Date().getTime(); -quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]); -let end = new Date().getTime(); -test('quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]) takes less than 2s to run', () => { - expect(end - start < 2000).toBeTruthy(); -}); From c244d9b90358fcf575a19296b34a5d33a12612ac Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:32:49 +0300 Subject: [PATCH 23/24] Remove binarySearch --- snippets_archive/binarySearch.md | 27 --------------------------- test/binarySearch.test.js | 17 ----------------- 2 files changed, 44 deletions(-) delete mode 100644 snippets_archive/binarySearch.md delete mode 100644 test/binarySearch.test.js diff --git a/snippets_archive/binarySearch.md b/snippets_archive/binarySearch.md deleted file mode 100644 index c68b19b87..000000000 --- a/snippets_archive/binarySearch.md +++ /dev/null @@ -1,27 +0,0 @@ ---- -title: binarySearch -tags: algorithm,beginner ---- - -Use recursion. Similar to `Array.prototype.indexOf()` that finds the index of a value within an array. -The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.prototype.indexOf()`. - -Search a sorted array by repeatedly dividing the search interval in half. -Begin with an interval covering the whole array. -If the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. -Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`. - -```js -const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { - if (start > end) return -1; - const mid = Math.floor((start + end) / 2); - if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1); - if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end); - return mid; -}; -``` - -```js -binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2 -binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1 -``` \ No newline at end of file diff --git a/test/binarySearch.test.js b/test/binarySearch.test.js deleted file mode 100644 index 4291f4885..000000000 --- a/test/binarySearch.test.js +++ /dev/null @@ -1,17 +0,0 @@ -const {binarySearch} = require('./_30s.js'); - -test('binarySearch is a Function', () => { - expect(binarySearch).toBeInstanceOf(Function); -}); -test('Finds item in array', () => { - expect(binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6)).toBe(2); -}); -test('Returns -1 when not found', () => { - expect(binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21)).toBe(-1); -}); -test('Works with empty arrays', () => { - expect(binarySearch([], 21)).toBe(-1); -}); -test('Works for one element arrays', () => { - expect(binarySearch([1], 1)).toBe(0); -}); From 6eb3e19bf7d617a9c8212a6697ae5510b9b2ca0e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 16 Apr 2020 11:32:59 +0300 Subject: [PATCH 24/24] Remove levenshteinDistance --- snippets_archive/levenshteinDistance.md | 42 ------------------------- test/levenshteinDistance.test.js | 14 --------- 2 files changed, 56 deletions(-) delete mode 100644 snippets_archive/levenshteinDistance.md delete mode 100644 test/levenshteinDistance.test.js diff --git a/snippets_archive/levenshteinDistance.md b/snippets_archive/levenshteinDistance.md deleted file mode 100644 index 646adc298..000000000 --- a/snippets_archive/levenshteinDistance.md +++ /dev/null @@ -1,42 +0,0 @@ ---- -title: levenshteinDistance -tags: algorithm,advanced ---- - -Calculates the [Levenshtein distance](https://en.wikipedia.org/wiki/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. - -```js -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]; -}; -``` - -```js -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)); -compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%) -``` \ No newline at end of file diff --git a/test/levenshteinDistance.test.js b/test/levenshteinDistance.test.js deleted file mode 100644 index 27f5fbddf..000000000 --- a/test/levenshteinDistance.test.js +++ /dev/null @@ -1,14 +0,0 @@ -const {levenshteinDistance} = require('./_30s.js'); - -test('levenshteinDistance is a Function', () => { - expect(levenshteinDistance).toBeInstanceOf(Function); -}); -test('levenshteinDistance returns the correct results', () => { - expect(levenshteinDistance('30-seconds-of-code', '30-seconds-of-python-code')).toBe(7); -}); -test('levenshteinDistance returns the correct result for 0-length string as first argument', () => { - expect(levenshteinDistance('', 'foo')).toBe(3); -}); -test('levenshteinDistance returns the correct result for 0-length string as second argument', () => { - expect(levenshteinDistance('bar', '')).toBe(3); -});