diff --git a/README.md b/README.md index 202571fa0..766363348 100644 --- a/README.md +++ b/README.md @@ -5569,7 +5569,7 @@ If the second parameter, `end`, is not specified, the range is considered to be ```js const inRange = (n, start, end = null) => { - if (end && start > end) end = [start, (start = end)][0]; + if (end && start > end) [end, start] = [start, end]; return end == null ? n >= 0 && n < start : n >= start && n < end; }; ``` @@ -5581,7 +5581,7 @@ const inRange = (n, start, end = null) => { inRange(3, 2, 5); // true inRange(3, 4); // true inRange(2, 3, 5); // false -inrange(3, 2); // false +inRange(3, 2); // false ``` diff --git a/docs/math.html b/docs/math.html index 014512c33..a3db3d026 100644 --- a/docs/math.html +++ b/docs/math.html @@ -167,13 +167,13 @@ own individual rating by supplying it as the third argument.
Calculates the Hamming distance between two values.
Use XOR operator (^) to find the bit difference between the two numbers, convert to a binary string using toString(2). Count and return the number of 1s in the string, using match(/1/g).
const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length;
hammingDistance(2, 3); // 1
Checks if the given number falls within 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 range is considered to be from 0 to start.
const inRange = (n, start, end = null) => { - if (end && start > end) end = [start, (start = end)][0]; + if (end && start > end) [end, start] = [start, end]; 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 +inRange(3, 2); // false
Checks if the first numeric argument is divisible by the second one.
Use the modulo operator (%) to check if the remainder is equal to 0.
const isDivisible = (dividend, divisor) => dividend % divisor === 0;
isDivisible(6, 3); // true
Returns true if the given number is even, false otherwise.
Checks whether a number is odd or even using the modulo (%) operator. Returns true if the number is even, false if the number is odd.
const isEven = num => num % 2 === 0;