From fd8ffcd2af9893360d3bf37202b8f590425ce006 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Sat, 29 Sep 2018 13:47:39 +0000 Subject: [PATCH] Travis build: 559 --- README.md | 146 +++++++++++++++++++++++++ docs/adapter.html | 2 +- docs/browser.html | 2 +- docs/date.html | 27 ++++- docs/function.html | 2 +- docs/index.html | 2 +- docs/math.html | 2 +- docs/node.html | 2 +- docs/object.html | 2 +- docs/string.html | 2 +- docs/type.html | 2 +- docs/utility.html | 2 +- snippets/compose.md | 5 +- snippets/degreesToRads.md | 2 +- snippets/getMeridiemSuffixOfInteger.md | 4 +- snippets/hz.md | 2 +- snippets/lcm.md | 2 +- snippets/luhnCheck.md | 2 +- snippets/percentile.md | 2 +- snippets/radsToDegrees.md | 2 +- 20 files changed, 194 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 20cc4a0f8..55648ff2f 100644 --- a/README.md +++ b/README.md @@ -251,10 +251,16 @@ average(1, 2, 3);
View contents +* [`dayOfYear`](#dayofyear) * [`formatDuration`](#formatduration) * [`getColonTimeFromDate`](#getcolontimefromdate) * [`getDaysDiffBetweenDates`](#getdaysdiffbetweendates) * [`getMeridiemSuffixOfInteger`](#getmeridiemsuffixofinteger) +* [`isAfterDate`](#isafterdate) +* [`isBeforeDate`](#isbeforedate) +* [`isSameDate`](#issamedate) +* [`maxDate`](#maxdate) +* [`minDate`](#mindate) * [`tomorrow`](#tomorrow)
@@ -4196,6 +4202,29 @@ UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d' ## ⏱️ Date +### dayOfYear + +Gets the day of the year from a `Date` object. + +Use `new Date()` and `Date.prototype.getFullYear()` to get the first day of the year as a `Date` object, subtract it from the provided `date` and divide with the milliseconds in each day to get the result. +Use `Math.floor()` to appropriately round the resulting day count to an integer. + +```js +const dayOfYear = date => + Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24); +``` + +
+Examples + +```js +dayOfYear(new Date()); // 272 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + ### formatDuration Returns the human readable format of the given number of milliseconds. @@ -4308,6 +4337,123 @@ getMeridiemSuffixOfInteger(25); // "1pm"
[⬆ Back to top](#table-of-contents) +### isAfterDate + +Check if a date is after another date. + +Use the greater than operator (`>`) to check if the first date comes after the second one. + +```js +const isAfterDate = (dateA, dateB) => dateA > dateB; +``` + +
+Examples + +```js +isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + +### isBeforeDate + +Check if a date is before another date. + +Use the less than operator (`<`) to check if the first date comes before the second one. + +```js +const isBeforeDate = (dateA, dateB) => dateA < dateB; +``` + +
+Examples + +```js +isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + +### isSameDate + +Check if a date is the same as another date. + +Use `Date.prototype.toISOString()` and strict equality checking (`===`) to check if the first date is the same as the second one. + +```js +const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString(); +``` + +
+Examples + +```js +isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + +### maxDate + +Returns the maximum of the given dates. + +Use `Math.max.apply()` to find the maximum date value, `new Date()` to convert it to a `Date` object. + +```js +const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates)); +``` + +
+Examples + +```js +const array = [ + new Date(2017, 4, 13), + new Date(2018, 2, 12), + new Date(2016, 0, 10), + new Date(2016, 0, 9) +]; +maxDate(array); // 2018-03-11T22:00:00.000Z +``` + +
+ +
[⬆ Back to top](#table-of-contents) + +### minDate + +Returns the minimum of the given dates. + +Use `Math.min.apply()` to find the minimum date value, `new Date()` to convert it to a `Date` object. + +```js +const minDate = (...dates) => new Date(Math.min.apply(null, ...dates)); +``` + +
+Examples + +```js +const array = [ + new Date(2017, 4, 13), + new Date(2018, 2, 12), + new Date(2016, 0, 10), + new Date(2016, 0, 9) +]; +minDate(array); // 2016-01-08T22:00:00.000Z +``` + +
+ +
[⬆ Back to top](#table-of-contents) + ### tomorrow Results in a string representation of tomorrow's date. diff --git a/docs/adapter.html b/docs/adapter.html index 68af67f75..313522f6d 100644 --- a/docs/adapter.html +++ b/docs/adapter.html @@ -91,7 +91,7 @@ },1700); } }, false); - }

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



Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.prototype.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

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



Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.prototype.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

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);
diff --git a/docs/browser.html b/docs/browser.html
index d90435991..49075c26d 100644
--- a/docs/browser.html
+++ b/docs/browser.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Browser

arrayToHtmlList

Converts the given array elements into <li> tags and appends them to the list of the given id.

Use Array.prototype.map(), document.querySelector(), and an anonymous inner closure to create a list of html tags.

const arrayToHtmlList = (arr, listID) =>
+      }

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



Browser

arrayToHtmlList

Converts the given array elements into <li> tags and appends them to the list of the given id.

Use Array.prototype.map(), document.querySelector(), and an anonymous inner closure to create a list of html tags.

const arrayToHtmlList = (arr, listID) =>
   (el => (
     (el = document.querySelector('#' + listID)),
     (el.innerHTML += arr.map(item => `<li>${item}</li>`).join(''))
diff --git a/docs/date.html b/docs/date.html
index 5700c9750..00a1595b7 100644
--- a/docs/date.html
+++ b/docs/date.html
@@ -91,7 +91,10 @@
             },1700);
           }
         }, false);
-      }

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



Date

formatDuration

Returns the human readable format of the given number of milliseconds.

Divide ms with the appropriate values to obtain the appropriate values for day, hour, minute, second and millisecond. Use Object.entries() with Array.prototype.filter() to keep only non-zero values. Use Array.prototype.map() to create the string for each value, pluralizing appropriately. Use String.prototype.join(', ') to combine the values into a string.

const formatDuration = ms => {
+      }

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



Date

dayOfYear

Gets the day of the year from a Date object.

Use new Date() and Date.prototype.getFullYear() to get the first day of the year as a Date object, subtract it from the provided date and divide with the milliseconds in each day to get the result. Use Math.floor() to appropriately round the resulting day count to an integer.

const dayOfYear = date =>
+  Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
+
dayOfYear(new Date()); // 272
+

formatDuration

Returns the human readable format of the given number of milliseconds.

Divide ms with the appropriate values to obtain the appropriate values for day, hour, minute, second and millisecond. Use Object.entries() with Array.prototype.filter() to keep only non-zero values. Use Array.prototype.map() to create the string for each value, pluralizing appropriately. Use String.prototype.join(', ') to combine the values into a string.

const formatDuration = ms => {
   if (ms < 0) ms = -ms;
   const time = {
     day: Math.floor(ms / 86400000),
@@ -124,6 +127,28 @@
 getMeridiemSuffixOfInteger(11); // "11am"
 getMeridiemSuffixOfInteger(13); // "1pm"
 getMeridiemSuffixOfInteger(25); // "1pm"
+

isAfterDate

Check if a date is after another date.

Use the greater than operator (>) to check if the first date comes after the second one.

const isAfterDate = (dateA, dateB) => dateA > dateB;
+
isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true
+

isBeforeDate

Check if a date is before another date.

Use the less than operator (<) to check if the first date comes before the second one.

const isBeforeDate = (dateA, dateB) => dateA < dateB;
+
isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true
+

isSameDate

Check if a date is the same as another date.

Use Date.prototype.toISOString() and strict equality checking (===) to check if the first date is the same as the second one.

const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
+
isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true
+

maxDate

Returns the maximum of the given dates.

Use Math.max.apply() to find the maximum date value, new Date() to convert it to a Date object.

const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates));
+
const array = [
+  new Date(2017, 4, 13),
+  new Date(2018, 2, 12),
+  new Date(2016, 0, 10),
+  new Date(2016, 0, 9)
+];
+maxDate(array); // 2018-03-11T22:00:00.000Z
+

minDate

Returns the minimum of the given dates.

Use Math.min.apply() to find the minimum date value, new Date() to convert it to a Date object.

const minDate = (...dates) => new Date(Math.min.apply(null, ...dates));
+
const array = [
+  new Date(2017, 4, 13),
+  new Date(2018, 2, 12),
+  new Date(2016, 0, 10),
+  new Date(2016, 0, 9)
+];
+minDate(array); // 2016-01-08T22:00:00.000Z
 

tomorrow

Results in a string representation of tomorrow's date. Use new Date() to get today's date, adding one day using Date.getDate() and Date.setDate(), and converting the Date object to a string.

const tomorrow = (long = false) => {
   let t = new Date();
   t.setDate(t.getDate() + 1);
diff --git a/docs/function.html b/docs/function.html
index 6df7294b0..ca48c82e0 100644
--- a/docs/function.html
+++ b/docs/function.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Function

attempt

Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.

Use a try... catch block to return either the result of the function or an appropriate error.

const attempt = (fn, ...args) => {
+      }

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



Function

attempt

Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.

Use a try... catch block to return either the result of the function or an appropriate error.

const attempt = (fn, ...args) => {
   try {
     return fn(...args);
   } catch (e) {
diff --git a/docs/index.html b/docs/index.html
index cbb6e6638..a7ea2b361 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Array

all

Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.

Use Array.prototype.every() to test if all elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const all = (arr, fn = Boolean) => arr.every(fn);
+      }

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



Array

all

Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.

Use Array.prototype.every() to test if all elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const all = (arr, fn = Boolean) => arr.every(fn);
 
all([4, 2, 3], x => x > 1); // true
 all([1, 2, 3]); // true
 

allEqual

Check if all elements in an array are equal.

Use Array.prototype.every() to check if all the elements of the array are the same as the first one.

const allEqual = arr => arr.every(val => val === arr[0]);
diff --git a/docs/math.html b/docs/math.html
index fb4d85cc9..22ca4aedf 100644
--- a/docs/math.html
+++ b/docs/math.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Math

approximatelyEqual

Checks if two numbers are approximately equal to each other.

Use Math.abs() to compare the absolute difference of the two values to epsilon. Omit the third parameter, epsilon, to use a default value of 0.001.

const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
+      }

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



Math

approximatelyEqual

Checks if two numbers are approximately equal to each other.

Use Math.abs() to compare the absolute difference of the two values to epsilon. Omit the third parameter, epsilon, to use a default value of 0.001.

const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
 
approximatelyEqual(Math.PI / 2.0, 1.5708); // true
 

average

Returns the average of two or more numbers.

Use Array.prototype.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
 
average(...[1, 2, 3]); // 2
diff --git a/docs/node.html b/docs/node.html
index c7fcd96f7..b5bd260a5 100644
--- a/docs/node.html
+++ b/docs/node.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Node

atob

Decodes a string of data which has been encoded using base-64 encoding.

Create a Buffer for the given string with base-64 encoding and use Buffer.toString('binary') to return the decoded string.

const atob = str => new Buffer(str, 'base64').toString('binary');
+      }

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



Node

atob

Decodes a string of data which has been encoded using base-64 encoding.

Create a Buffer for the given string with base-64 encoding and use Buffer.toString('binary') to return the decoded string.

const atob = str => new Buffer(str, 'base64').toString('binary');
 
atob('Zm9vYmFy'); // 'foobar'
 

btoa

Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

Create a Buffer for the given string with binary encoding and use Buffer.toString('base64') to return the encoded string.

const btoa = str => new Buffer(str, 'binary').toString('base64');
 
btoa('foobar'); // 'Zm9vYmFy'
diff --git a/docs/object.html b/docs/object.html
index ca173bd3e..66396c8e6 100644
--- a/docs/object.html
+++ b/docs/object.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Object

bindAll

Binds methods of an object to the object itself, overwriting the existing method.

Use Array.prototype.forEach() to return a function that uses Function.prototype.apply() to apply the given context (obj) to fn for each function specified.

const bindAll = (obj, ...fns) =>
+      }

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



Object

bindAll

Binds methods of an object to the object itself, overwriting the existing method.

Use Array.prototype.forEach() to return a function that uses Function.prototype.apply() to apply the given context (obj) to fn for each function specified.

const bindAll = (obj, ...fns) =>
   fns.forEach(
     fn => (
       (f = obj[fn]),
diff --git a/docs/string.html b/docs/string.html
index 878658c1b..ecba2e8b4 100644
--- a/docs/string.html
+++ b/docs/string.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



String

byteSize

Returns the length of a string in bytes.

Convert a given string to a Blob Object and find its size.

const byteSize = str => new Blob([str]).size;
+      }

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



String

byteSize

Returns the length of a string in bytes.

Convert a given string to a Blob Object and find its size.

const byteSize = str => new Blob([str]).size;
 
byteSize('😀'); // 4
 byteSize('Hello World'); // 11
 

capitalize

Capitalizes the first letter of a string.

Use array destructuring and String.prototype.toUpperCase() to capitalize first letter, ...rest to get array of characters after first letter and then Array.prototype.join('') to make it a string again. Omit the lowerRest parameter to keep the rest of the string intact, or set it to true to convert to lowercase.

const capitalize = ([first, ...rest], lowerRest = false) =>
diff --git a/docs/type.html b/docs/type.html
index bc8efacea..8d52b4bf3 100644
--- a/docs/type.html
+++ b/docs/type.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Type

getType

Returns the native type of a value.

Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null.

const getType = v =>
+      }

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



Type

getType

Returns the native type of a value.

Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null.

const getType = v =>
   v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
 
getType(new Set([1, 2, 3])); // 'set'
 

is

Checks if the provided value is of the specified type.

Ensure the value is not undefined or null using Array.prototype.includes(), and compare the constructor property on the value with type to check if the provided value is of the specified type.

const is = (type, val) => ![, null].includes(val) && val.constructor === type;
diff --git a/docs/utility.html b/docs/utility.html
index 7b8b6ea88..cac9c8720 100644
--- a/docs/utility.html
+++ b/docs/utility.html
@@ -91,7 +91,7 @@
             },1700);
           }
         }, false);
-      }

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



Utility

castArray

Casts the provided value as an array if it's not one.

Use Array.prototype.isArray() to determine if val is an array and return it as-is or encapsulated in an array accordingly.

const castArray = val => (Array.isArray(val) ? val : [val]);
+      }

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



Utility

castArray

Casts the provided value as an array if it's not one.

Use Array.prototype.isArray() to determine if val is an array and return it as-is or encapsulated in an array accordingly.

const castArray = val => (Array.isArray(val) ? val : [val]);
 
castArray('foo'); // ['foo']
 castArray([1]); // [1]
 

cloneRegExp

Clones a regular expression.

Use new RegExp(), RegExp.source and RegExp.flags to clone the given regular expression.

const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);
diff --git a/snippets/compose.md b/snippets/compose.md
index b1788140a..778e10eef 100644
--- a/snippets/compose.md
+++ b/snippets/compose.md
@@ -12,6 +12,9 @@ const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
 ```js
 const add5 = x => x + 5;
 const multiply = (x, y) => x * y;
-const multiplyAndAdd5 = compose(add5, multiply);
+const multiplyAndAdd5 = compose(
+  add5,
+  multiply
+);
 multiplyAndAdd5(5, 2); // 15
 ```
diff --git a/snippets/degreesToRads.md b/snippets/degreesToRads.md
index ed3a2ee0e..90790765d 100644
--- a/snippets/degreesToRads.md
+++ b/snippets/degreesToRads.md
@@ -5,7 +5,7 @@ Converts an angle from degrees to radians.
 Use `Math.PI` and the degree to radian formula to convert the angle from degrees to radians.
 
 ```js
-const degreesToRads = deg => deg * Math.PI / 180.0;
+const degreesToRads = deg => (deg * Math.PI) / 180.0;
 ```
 
 ```js
diff --git a/snippets/getMeridiemSuffixOfInteger.md b/snippets/getMeridiemSuffixOfInteger.md
index ea153f7b2..9cf6e46c3 100644
--- a/snippets/getMeridiemSuffixOfInteger.md
+++ b/snippets/getMeridiemSuffixOfInteger.md
@@ -11,8 +11,8 @@ const getMeridiemSuffixOfInteger = num =>
     : num === 12
       ? 12 + 'pm'
       : num < 12
-        ? num % 12 + 'am'
-        : num % 12 + 'pm';
+        ? (num % 12) + 'am'
+        : (num % 12) + 'pm';
 ```
 
 ```js
diff --git a/snippets/hz.md b/snippets/hz.md
index 08cff5de6..c1e9c0ceb 100644
--- a/snippets/hz.md
+++ b/snippets/hz.md
@@ -11,7 +11,7 @@ Omit the second argument, `iterations`, to use the default of 100 iterations.
 const hz = (fn, iterations = 100) => {
   const before = performance.now();
   for (let i = 0; i < iterations; i++) fn();
-  return 1000 * iterations / (performance.now() - before);
+  return (1000 * iterations) / (performance.now() - before);
 };
 ```
 
diff --git a/snippets/lcm.md b/snippets/lcm.md
index 74f346af3..970e3083c 100644
--- a/snippets/lcm.md
+++ b/snippets/lcm.md
@@ -8,7 +8,7 @@ The GCD formula uses recursion.
 ```js
 const lcm = (...arr) => {
   const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  const _lcm = (x, y) => x * y / gcd(x, y);
+  const _lcm = (x, y) => (x * y) / gcd(x, y);
   return [...arr].reduce((a, b) => _lcm(a, b));
 };
 ```
diff --git a/snippets/luhnCheck.md b/snippets/luhnCheck.md
index 9c8bf0152..c99532ed1 100644
--- a/snippets/luhnCheck.md
+++ b/snippets/luhnCheck.md
@@ -15,7 +15,7 @@ const luhnCheck = num => {
     .reverse()
     .map(x => parseInt(x));
   let lastDigit = arr.splice(0, 1)[0];
-  let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + (val * 2) % 9 || 9), 0);
+  let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
   sum += lastDigit;
   return sum % 10 === 0;
 };
diff --git a/snippets/percentile.md b/snippets/percentile.md
index 1b2276af7..3522a67d8 100644
--- a/snippets/percentile.md
+++ b/snippets/percentile.md
@@ -6,7 +6,7 @@ Use `Array.prototype.reduce()` to calculate how many numbers are below the value
 
 ```js
 const percentile = (arr, val) =>
-  100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
+  (100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0)) / arr.length;
 ```
 
 ```js
diff --git a/snippets/radsToDegrees.md b/snippets/radsToDegrees.md
index d8dd54958..3cd4fa8d4 100644
--- a/snippets/radsToDegrees.md
+++ b/snippets/radsToDegrees.md
@@ -5,7 +5,7 @@ Converts an angle from radians to degrees.
 Use `Math.PI` and the radian to degree formula to convert the angle from radians to degrees.
 
 ```js
-const radsToDegrees = rad => rad * 180.0 / Math.PI;
+const radsToDegrees = rad => (rad * 180.0) / Math.PI;
 ```
 
 ```js