diff --git a/README.md b/README.md index d3cabfac7..4e447bf9e 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ * [`union`](#union) * [`without`](#without) * [`zip`](#zip) +* [`zipObject`](#zipobject) ### Browser * [`arrayToHtmlList`](#arraytohtmllist) @@ -81,6 +82,7 @@ ### Math * [`arrayAverage`](#arrayaverage) * [`arraySum`](#arraysum) +* [`clampNumber`](#clampnumber) * [`collatz`](#collatz) * [`digitize`](#digitize) * [`distance`](#distance) @@ -88,6 +90,7 @@ * [`fibonacci`](#fibonacci) * [`gcd`](#gcd) * [`hammingDistance`](#hammingdistance) +* [`inRange`](#inrange) * [`isArmstrongNumber`](#isarmstrongnumber) * [`isDivisible`](#isdivisible) * [`isEven`](#iseven) @@ -782,6 +785,20 @@ const zip = (...arrays) => { //zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]] ``` +[⬆ back to top](#table-of-contents) + +### zipObject + +Given an array of valid property identifiers and an array of values, return an object associating the properties to the values. + +Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using `Array.reduce()`. + +```js +const zipObject = ( props, values ) => props.reduce( ( obj, prop, index ) => ( obj[prop] = values[index], obj ), {} ) +// zipObject(['a','b','c'], [1,2]) -> {a: 1, b: 2, c: undefined} +// zipObject(['a','b'], [1,2,3]) -> {a: 1, b: 2} +``` + [⬆ back to top](#table-of-contents) ## Browser @@ -1135,6 +1152,26 @@ const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); [⬆ back to top](#table-of-contents) +### clampNumber + +Clamps `num` within the inclusive `lower` and `upper` bounds. + +If `lower` is greater than `upper`, swap them. +If `num` falls within the range, return `num`. +Otherwise return the nearest number in the range. + +```js +const clampNumber = (num, lower, upper) => { + if(lower > upper) upper = [lower, lower = upper][0]; + return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper) +} +// clampNumber(2, 3, 5) -> 3 +// clampNumber(1, -1, -5) -> -1 +// clampNumber(3, 2, 4) -> 3 +``` + +[⬆ back to top](#table-of-contents) + ### collatz Applies the Collatz algorithm. @@ -1239,6 +1276,26 @@ const hammingDistance = (num1, num2) => [⬆ back to top](#table-of-contents) +### inRange + +Checks if the given number falls in the given range. + +Use arithmetic comparison to check if the given number is in the specified range. +If the second parameter, `end`, is not specified, the reange is considered to be from `0` to `start`. + +```js +const inRange = (n, start, end=null) => { + if(end && start > end) end = [start, start=end][0]; + return (end == null) ? (n>=0 && n=start && n true +// inRange(3, 4) -> true +// inRange(2, 3, 5) -> false +// inrange(3, 2) -> false +``` + +[⬆ back to top](#table-of-contents) + ### isArmstrongNumber Checks if the given number is an armstrong number or not. diff --git a/docs/index.html b/docs/index.html index eb3335d43..42421208d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -83,6 +83,7 @@ union without zip +zipObject

Browser

arrayToHtmlList @@ -112,6 +113,7 @@

Math

arrayAverage arraySum +clampNumber collatz digitize distance @@ -119,6 +121,7 @@ fibonacci gcd hammingDistance +inRange isArmstrongNumber isDivisible isEven @@ -525,6 +528,13 @@ If lengths of the argument-arrays vary, undefined is used where no //zip(['a', 'b'], [1, 2], [true, false]); -> [['a', 1, true], ['b', 2, false]] //zip(['a'], [1, 2], [true, false]); -> [['a', 1, true], [undefined, 2, false]] +

zipObject

+

Given an array of valid property identifiers and an array of values, return an object associating the properties to the values.

+

Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using Array.reduce().

+
const zipObject = ( props, values ) => props.reduce( ( obj, prop, index ) => ( obj[prop] = values[index], obj ), {} )
+// zipObject(['a','b','c'], [1,2]) -> {a: 1, b: 2, c: undefined}
+// zipObject(['a','b'], [1,2,3]) -> {a: 1, b: 2}
+

Browser

arrayToHtmlList

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

@@ -728,6 +738,19 @@ async function sleepyWork() {
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
 // arraySum([1,2,3,4]) -> 10
 
+

clampNumber

+

Clamps num within the inclusive lower and upper bounds.

+

If lower is greater than upper, swap them. +If num falls within the range, return num. +Otherwise return the nearest number in the range.

+
const clampNumber = (num, lower, upper) => {
+  if(lower > upper) upper = [lower, lower = upper][0];
+  return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper) 
+}
+// clampNumber(2, 3, 5) -> 3
+// clampNumber(1, -1, -5) -> -1
+// clampNumber(3, 2, 4) -> 3
+

collatz

Applies the Collatz algorithm.

If n is even, return n/2. Otherwise return 3n+1.

@@ -783,6 +806,19 @@ Count and return the number of 1s in the string, using match( ((num1 ^ num2).toString(2).match(/1/g) || '').length; // hammingDistance(2,3) -> 1 +

inRange

+

Checks if the given number falls in the given range.

+

Use arithmetic comparison to check if the given number is in the specified range. +If the second parameter, end, is not specified, the reange is considered to be from 0 to start.

+
const inRange = (n, start, end=null) => {
+  if(end && start > end) end = [start, start=end][0];
+  return (end == null) ? (n>=0 && n<start) : (n>=start && n<end);
+}
+// inRange(3, 2, 5) -> true
+// inRange(3, 4) -> true
+// inRange(2, 3, 5) -> false
+// inrange(3, 2) -> false
+

isArmstrongNumber

Checks if the given number is an armstrong number or not.

Convert the given number into array of digits. Use Math.pow() 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.

diff --git a/tag_database b/tag_database index a0a06d2d0..84063917c 100644 --- a/tag_database +++ b/tag_database @@ -10,6 +10,7 @@ capitalize:string capitalizeEveryWord:string chainAsync:function chunk:array +clampNumber:math cleanObj:object coalesce:utility coalesceFactory:utility @@ -52,6 +53,7 @@ initial:array initialize2DArray:array initializeArrayWithRange:array initializeArrayWithValues:array +inRange:math intersection:array isArmstrongNumber:math isArray:utility