diff --git a/README.md b/README.md index 3189f39ca..0f083e4a2 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ average(1, 2, 3); * [`average`](#average) * [`averageBy`](#averageby) * [`clampNumber`](#clampnumber) +* [`degreesToRads`](#degreestorads) * [`digitize`](#digitize) * [`distance`](#distance) * [`elo`](#elo-) @@ -281,6 +282,7 @@ average(1, 2, 3); * [`percentile`](#percentile) * [`powerset`](#powerset) * [`primes`](#primes) +* [`radsToDegrees`](#radstodegrees) * [`randomIntArrayInRange`](#randomintarrayinrange) * [`randomIntegerInRange`](#randomintegerinrange) * [`randomNumberInRange`](#randomnumberinrange) @@ -4383,6 +4385,28 @@ clampNumber(1, -1, -5); // -1
[⬆ Back to top](#table-of-contents) +### degreesToRads + +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; +``` + +
+Examples + +```js +degreesToRads(90.0); // ~1.5708 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### digitize Converts a number to an array of digits. @@ -4931,6 +4955,28 @@ primes(10); // [2,3,5,7]
[⬆ Back to top](#table-of-contents) +### radsToDegrees + +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; +``` + +
+Examples + +```js +radsToDegrees(Math.PI / 2); // 90 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### randomIntArrayInRange Returns an array of n random integers in the specified range. diff --git a/docs/index.html b/docs/index.html index 1da3415cf..7aa7f51ad 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, 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.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.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);
@@ -1000,6 +1000,8 @@ console.log<
 

clampNumber

Clamps num within the inclusive range specified by the boundary values a and b.

If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
 
clampNumber(2, 3, 5); // 3
 clampNumber(1, -1, -5); // -1
+

degreesToRads

Converts an angle from degrees to radians.

Use Math.PI and the degree to radian formula to convert the angle from degrees to radians.

const degreesToRads = deg => deg * Math.PI / 180.0;
+
degreesToRads(90.0); // ~1.5708
 

digitize

Converts a number to an array of digits.

Convert the number to a string, using the spread operator (...) to build an array. Use Array.map() and parseInt() to transform each value to an integer.

const digitize = n => [...`${n}`].map(i => parseInt(i));
 
digitize(123); // [1, 2, 3]
 

distance

Returns the distance between two points.

Use Math.hypot() to calculate the Euclidean distance between two points.

const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
@@ -1123,6 +1125,8 @@ own individual rating by supplying it as the third argument.
   return arr;
 };
 
primes(10); // [2,3,5,7]
+

radsToDegrees

Converts an angle from radians to degrees.

Use Math.PI and the radian to degree formula to convert the angle from radians to degrees.

const radsToDegrees = rad => rad * 180.0 / Math.PI;
+
radsToDegrees(Math.PI / 2); // 90
 

randomIntArrayInRange

Returns an array of n random integers in the specified range.

Use Array.from() to create an empty array of the specific length, Math.random() to generate a random number and map it to the desired range, using Math.floor() to make it an integer.

const randomIntArrayInRange = (min, max, n = 1) =>
   Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
 
randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ]