数学

求和

1. sum

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
1

EXAMPLES:

sum(1, 2, 3, 4); // 10
sum(...[1, 2, 3, 4]); // 10
1
2

2. sumPower

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const sumPower = (end, power = 2, start = 1) =>
  Array(end + 1 - start)
    .fill(0)
    .map((x, i) => (i + start) ** power)
    .reduce((a, b) => a + b, 0);
1
2
3
4
5

EXAMPLES:

sumPower(10); // 385
sumPower(10, 3); // 3025
sumPower(10, 3, 5); // 2925
1
2
3

3. accumulate

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const accumulate = (...nums) => nums.reduce((acc, n) => [...acc, n + +acc.slice(-1)],[]);
1

EXAMPLES:

accumulate(1, 2, 3, 4); // [1, 3, 6, 10]
accumulate(...[1, 2, 3, 4]); // [1, 3, 6, 10]
1
2

4. distance(平方和后平方根)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
1

EXAMPLES:

distance(1, 1, 2, 3); // 2.23606797749979
1

5. factorial(递归)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const factorial = n =>
  n < 0
    ? (() => {
        throw new TypeError('Negative numbers are not allowed!');
      })()
    : n <= 1
    ? 1
    : n * factorial(n - 1);
1
2
3
4
5
6
7
8

EXAMPLES:

factorial(6); // 720
1

均值

1. average

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
1

EXAMPLES:

average(...[1, 2, 3]); // 2
average(1, 2, 3); // 2
1
2

2. averageBy(by)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const averageBy = (arr, fn) =>
  arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
  arr.length;
1
2
3

EXAMPLES:

averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
1
2

相等

1. approximatelyEqual(约)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

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

EXAMPLES:

approximatelyEqual(Math.PI / 2.0, 1.5708); // true
1

2. isNegativeZero(-0)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const isNegativeZero = val => val === 0 && 1 / val === -Infinity;
1

EXAMPLES:

isNegativeZero(-0); // true
isNegativeZero(0); // false
1
2

3. isPowerOfTwo(2的幂)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const isPowerOfTwo = n => !!n && (n & (n - 1)) == 0;
1

EXAMPLES:

isPowerOfTwo(0); // false
isPowerOfTwo(1); // true
isPowerOfTwo(8); // true
1
2
3

随机

1. randomHexColorCode(颜色)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const randomHexColorCode = () => {
  let n = (Math.random() * 0xfffff * 1000000).toString(16);
  return '#' + n.slice(0, 6);
};
1
2
3
4

EXAMPLES:

randomHexColorCode(); // "#e34155"
1

2. randomIntArrayInRange(区间)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const randomIntArrayInRange = (min, max, n = 1) =>
  Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
1
2

EXAMPLES:

randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ]
1

区间

1. clampNumber(更近)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const clampNumber = (num, [a, b]) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
1

EXAMPLES:

clampNumber(2, [3, 5]); // 3
clampNumber(1, [-1, -5]); // -1
1
2

2. geometricProgression(生成)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

初看时有点懵。

FUNCTION:

const geometricProgression = (end, start = 1, step = 2) =>
  Array.from({ length: Math.floor(Math.log(end / start) / Math.log(step)) + 1 }).map(
    (v, i) => start * step ** i
  );
1
2
3
4

EXAMPLES:

geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]
geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]
geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]
1
2
3

3. inRange(判断)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const inRange = (n, start, end = null) => {
  if (end && start > end) [end, start] = [start, end];
  return end == null ? n >= 0 && n < start : n >= start && n < end;
};
1
2
3
4

EXAMPLES:

inRange(3, 2, 5); // true
inRange(3, 4); // true
inRange(2, 3, 5); // false
inRange(3, 2); // false
1
2
3
4

4. mapNumRange(区间映射)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
  ((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
1
2

EXAMPLES:

mapNumRange(5, 0, 10, 0, 100); // 50
1

5. toSafeInteger(安全整数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const toSafeInteger = num =>
  Math.round(Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER));
1
2

EXAMPLES:

toSafeInteger('3.2'); // 3
toSafeInteger(Infinity); // 9007199254740991
1
2

向量

1. vectorAngle(角度)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const vectorAngle = (x, y) => {
  let mX = Math.sqrt(x.reduce((acc, n) => acc + Math.pow(n, 2), 0));
  let mY = Math.sqrt(y.reduce((acc, n) => acc + Math.pow(n, 2), 0));
  return Math.acos(x.reduce((acc, n, i) => acc + n * y[i], 0) / (mX * mY));
};
1
2
3
4
5

EXAMPLES:

vectorAngle([3, 4], [4, 3]); // 0.283794109208328
1

2. vectorDistance(距离)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const vectorDistance = (x, y) =>
  Math.sqrt(x.reduce((acc, val, i) => acc + Math.pow(val - y[i], 2), 0));
1
2

EXAMPLES:

vectorDistance([10, 0, 5], [20, 0, 10]); // 11.180339887498949
1

转换

1. celsiusToFahrenheit(°C => °F)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const celsiusToFahrenheit = degrees => 1.8 * degrees + 32;
1

EXAMPLES:

celsiusToFahrenheit(33); // 91.4
1

2. fahrenheitToCelsius(°F => °C)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const fahrenheitToCelsius = degrees => ((degrees - 32) * 5) / 9;
1

EXAMPLES:

fahrenheitToCelsius(32); // 0
1

3. degreesToRads(角度 => 弧度)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const degreesToRads = deg => (deg * Math.PI) / 180.0;
1

EXAMPLES:

degreesToRads(90.0); // ~1.5708
1

4. radsToDegrees(弧度 => 角度)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const radsToDegrees = rad => (rad * 180.0) / Math.PI;
1

EXAMPLES:

radsToDegrees(Math.PI / 2); // 90
1

5. toCurrency(货币)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const toCurrency = (n, curr, LanguageFormat = undefined) =>
  Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n);
1
2

EXAMPLES:

toCurrency(123456.789, 'EUR'); // €123,456.79  | currency: Euro | currencyLangFormat: Local
toCurrency(123456.789, 'USD', 'en-us'); // $123,456.79  | currency: US Dollar | currencyLangFormat: English (United States)
toCurrency(123456.789, 'USD', 'fa'); // ۱۲۳٬۴۵۶٫۷۹ ؜$ | currency: US Dollar | currencyLangFormat: Farsi
toCurrency(322342436423.2435, 'JPY'); // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local
toCurrency(322342436423.2435, 'JPY', 'fi'); // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: Finnish
1
2
3
4
5

6. toOrdinalSuffix(序数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const toOrdinalSuffix = num => {
  const int = parseInt(num),
    digits = [int % 10, int % 100],
    ordinals = ['st', 'nd', 'rd', 'th'],
    oPattern = [1, 2, 3, 4],
    tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
  return oPattern.includes(digits[0]) && !tPattern.includes(digits[1])
    ? int + ordinals[digits[0] - 1]
    : int + ordinals[3];
};
1
2
3
4
5
6
7
8
9
10

EXAMPLES:

toOrdinalSuffix('123'); // "123rd"
1

7. digitize(string => number)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const digitize = n => [...`${n}`].map(i => parseInt(i));
1

EXAMPLES:

digitize(123); // [1, 2, 3]
1

8. sdbm

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const sdbm = str => {
  let arr = str.split('');
  return arr.reduce(
    (hashCode, currentVal) =>
      (hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode),
    0
  );
};
1
2
3
4
5
6
7
8

EXAMPLES:

sdbm('name'); // -3521204949
1

Math

1. binomialCoefficient(二项式系数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const binomialCoefficient = (n, k) => {
  if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
  if (k < 0 || k > n) return 0;
  if (k === 0 || k === n) return 1;
  if (k === 1 || k === n - 1) return n;
  if (n - k < k) k = n - k;
  let res = n;
  for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
  return Math.round(res);
};
1
2
3
4
5
6
7
8
9
10

EXAMPLES:

binomialCoefficient(8, 2); // 28
1

2. elo

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

等级分 (opens new window) 的实现。

FUNCTION:

const elo = ([...ratings], kFactor = 32, selfRating) => {
  const [a, b] = ratings;
  const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
  const newRating = (rating, i) =>
    (selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
  if (ratings.length === 2) return [newRating(a, 1), newRating(b, 0)];

  for (let i = 0, len = ratings.length; i < len; i++) {
    let j = i;
    while (j < len - 1) {
      j++;
      [ratings[i], ratings[j]] = elo([ratings[i], ratings[j]], kFactor);
    }
  }
  return ratings;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

EXAMPLES:

// Standard 1v1s
elo([1200, 1200]); // [1216, 1184]
elo([1200, 1200], 64); // [1232, 1168]
// 4 player FFA, all same rank
elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154]
/*
For teams, each rating can adjusted based on own team's average rating vs.
average rating of opposing team, with the score being added to their
own individual rating by supplying it as the third argument.
*/
1
2
3
4
5
6
7
8
9
10

3. fibonacci(斐波那契数列)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const fibonacci = n =>
  Array.from({ length: n }).reduce(
    (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
    []
  );
1
2
3
4
5

EXAMPLES:

fibonacci(6); // [0, 1, 1, 2, 3, 5]
1

4. gcd(最大公约数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const gcd = (...arr) => {
  const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
  return [...arr].reduce((a, b) => _gcd(a, b));
};
1
2
3
4

EXAMPLES:

gcd(8, 36); // 4
gcd(...[12, 8, 32]); // 4
1
2

5. lcm(最小公倍数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const lcm = (...arr) => {
  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
  const _lcm = (x, y) => (x * y) / gcd(x, y);
  return [...arr].reduce((a, b) => _lcm(a, b));
};
1
2
3
4
5

EXAMPLES:

lcm(12, 7); // 84
lcm(...[1, 3, 4, 5]); // 60
1
2

6. hammingDistance(汉明距离)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

^ 按位异或操作。

FUNCTION:

const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length;
1

EXAMPLES:

hammingDistance(2, 3); // 1
1

7. isPrime(质数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const isPrime = num => {
  const boundary = Math.floor(Math.sqrt(num));
  for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
  return num >= 2;
};
1
2
3
4
5

EXAMPLES:

isPrime(11); // true
1

8. primes(质数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const primes = num => {
  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;
};
1
2
3
4
5
6
7

EXAMPLES:

primes(10); // [2,3,5,7]
1

9. median(中位数)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const median = arr => {
  const mid = Math.floor(arr.length / 2),
    nums = [...arr].sort((a, b) => a - b);
  return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
1
2
3
4
5

EXAMPLES:

median([5, 6, 50, 1, -5]); // 5
1

10. standardDeviation(标准差)

FROM

30 seconds of code (Math) (opens new window)

DETAIL:

暂无。

FUNCTION:

const standardDeviation = (arr, usePopulation = false) => {
  const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
  return Math.sqrt(
    arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) /
      (arr.length - (usePopulation ? 0 : 1))
  );
};
1
2
3
4
5
6
7

EXAMPLES:

standardDeviation([10, 2, 38, 23, 38, 23, 21]); // 13.284434142114991 (sample)
standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
1
2
最后更新时间: 1 年前