diff --git a/docs/index.html b/docs/index.html index 12e0b5d12..07111b089 100644 --- a/docs/index.html +++ b/docs/index.html @@ -14,11 +14,10 @@ - -
+Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.
const arrayLcm = 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));
}
// arrayLcm([1,2,3,4,5]) -> 60
@@ -448,7 +449,7 @@ Use Array.reduce() to create an object, where the keys are produced
Initializes an array containing the numbers in the specified range where start and end are inclusive.
Use Array((end + 1) - start) to create an array of the desired length, Array.map() to fill with the desired values in a range.
You can omit start to use a default value of 0.
-const initializeArrayWithRange = (end, start = 0) =>
+const initializeArrayWithRange = (end, start = 0) =>
Array.from({ length: (end + 1) - start }).map((v, i) => i + start);
// initializeArrayWithRange(5) -> [0,1,2,3,4,5]
// initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
@@ -475,7 +476,7 @@ You can omit value to use a default value of 0.
mapObject
Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value.
Use an anonymous inner function scope to declare an undefined memory space, using closures to store a return value. Use a new Array to store the array with a map of the function over its data set and a comma operator to return a second step, without needing to move from one context to another (due to closures and order of operations).
-const mapObject = (arr, fn) =>
+const mapObject = (arr, fn) =>
(a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( );
/*
const squareIt = arr => mapObject(arr, a => a*a)
@@ -506,7 +507,7 @@ Use Array.length = 0 to mutate the passed in an array by resetting
const pull = (arr, ...args) => {
let argState = Array.isArray(args[0]) ? args[0] : args;
let pulled = arr.filter((v, i) => !argState.includes(v));
- arr.length = 0;
+ arr.length = 0;
pulled.forEach(v => arr.push(v));
};
@@ -527,7 +528,7 @@ Use Array.push() to keep track of pulled values
let removed = [];
let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v)
.filter((v, i) => !pullArr.includes(i))
- arr.length = 0;
+ arr.length = 0;
pulled.forEach(v => arr.push(v));
return removed;
}
@@ -544,7 +545,7 @@ Use Array.push() to keep track of pulled values
Use Array.length = 0 to mutate the passed in an array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.
Use Array.push() to keep track of pulled values
const pullAtValue = (arr, pullArr) => {
- let removed = [],
+ let removed = [],
pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v),
mutateTo = arr.filter((v, i) => !pullArr.includes(v));
arr.length = 0;
@@ -856,7 +857,7 @@ 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)
+ return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper)
}
// clampNumber(2, 3, 5) -> 3
// clampNumber(1, -1, -5) -> -1
@@ -951,7 +952,7 @@ If the second parameter, end, is not specified, the range is consid
isArmstrongNumber
Checks if the given number is an Armstrong number or not.
Convert the given number into an 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.
-const isArmstrongNumber = digits =>
+const isArmstrongNumber = digits =>
( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) );
// isArmstrongNumber(1634) -> true
// isArmstrongNumber(371) -> true
@@ -1031,13 +1032,13 @@ Then, split('') into individual characters, reverse(),
Generates primes up to a given number, using the Sieve of Eratosthenes.
Generate an array from 2 to the given number. Use Array.filter() to filter out the values divisible by any number from 2 to the square root of the provided number.
const primes = num => {
- let arr = Array.from({length:num-1}).map((x,i)=> i+2),
+ let arr = Array.from({length:num-1}).map((x,i)=> i+2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2);
numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x)));
- return arr;
+ return arr;
}
-// primes(10) -> [2,3,5,7]
+// primes(10) -> [2,3,5,7]
randomIntegerInRange
Returns a random integer in the specified range.
@@ -1432,7 +1433,6 @@ Use Number() to check if the coercion holds.
-