mirror of
https://github.com/Fechin/reference.git
synced 2026-11-22 05:51:48 +03:00
Release the latest generator
This commit is contained in:
1 parent
b520119d38
commit
15c93e54ee
484 files changed
+4969
-856
No files matched your search
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: How to add an ordinal suffix to a number in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to add an ordinal suffix to a number only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const addOrdinal = n => `${n}${['st', 'nd', 'rd'][((n + 90) % 100 - 10) % 10 - 1] || 'th'}`;
|
||||
```
|
||||
Parameters `n` is a position number.
|
||||
|
||||
|
||||
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const addOrdinal = n => `${n}${[, 'st', 'nd', 'rd'][/1?.$/.exec(n)] || 'th'}`;
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const addOrdinal = n => `${n}${[, 'st', 'nd', 'rd'][n % 100 >> 3^1 && n % 10] || 'th'}`;
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const addOrdinal = n => `${n}${{one: 'st', two: 'nd', few: 'rd', other: 'th'}[new Intl.PluralRules('en', { type: 'ordinal' }).select(n)]}`;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
addOrdinal(1); // '1st'
|
||||
addOrdinal(2); // '2nd'
|
||||
addOrdinal(3); // '3rd'
|
||||
addOrdinal(11); // '11th'
|
||||
addOrdinal(12); // '13th'
|
||||
addOrdinal(13); // '13th'
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to calculate fibonacci numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate fibonacci numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const fibo = (n, memo = {}) => memo[n] || (n <= 2 ? 1 : (memo[n] = fibo(n - 1, memo) + fibo(n - 2, memo)));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
fibo(1); // 1
|
||||
fibo(2); // 1
|
||||
fibo(3); // 2
|
||||
fibo(4); // 3
|
||||
fibo(5); // 5
|
||||
fibo(6); // 8
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to calculate the average of arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the average of arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const average = (...args) => args.reduce((a, b) => a + b) / args.length;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
average(1, 2, 3, 4); // 2.5
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to calculate the division of arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the division of arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const division = (...args) => args.reduce((a, b) => a / b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
division(1, 2, 3, 4); // 0.04166666666666666
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: How to calculate the factorial of a number in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the factorial of a number only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
factorial(2); // 2
|
||||
factorial(3); // 6
|
||||
factorial(4); // 24
|
||||
factorial(5); // 120
|
||||
factorial(6); // 720
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to calculate the mod of collection index in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the mod of collection index only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const mod = (a, b) => ((a % b) + b) % b;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
mod(-1, 5); // 4
|
||||
mod(3, 5); // 3
|
||||
mod(6, 5); // 1
|
||||
```
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to calculate the remainder of division of arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the remainder of division of arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const remainder = (...args) => args.reduce((a, b) => a % b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
remainder(1, 2, 3, 4); // 1
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to calculate the sum of arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to calculate the sum of arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const sum = (...args) => args.reduce((a, b) => a + b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
sum(1, 2, 3, 4); // 10
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to clamp a number between two values in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to clamp a number between two values only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const clamp = (val, min = 0, max = 1) => Math.max(min, Math.min(max, val));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
clamp(199, 10, 25); // 25
|
||||
```
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to compute the greatest common divisor between two numbers in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compute the greatest common divisor between two numbers only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
gcd(10, 15); // 5
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: How to convert a number to equivalent characters in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert a number to equivalent characters only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toChars = n => `${n >= 26 ? toChars(Math.floor(n / 26) - 1) : ''}${'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[n % 26]}`;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toChars(0); // A
|
||||
toChars(1); // B
|
||||
toChars(25); // Z
|
||||
|
||||
toChars(26); // AA
|
||||
toChars(27); // AB
|
||||
toChars(51); // AZ
|
||||
|
||||
toChars(701); // ZZ
|
||||
toChars(702); // AAA
|
||||
toChars(703); // AAB
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to convert a string to number in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert a string to number only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toNumber = str => +str;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toNumber('42'); // 42
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to convert decimal to binary recursively in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to convert decimal to binary recursively only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const decToBi = num => num === 0 ? 0 : num % 2 + 10 * decToBi(~~(num / 2));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
decToBi(10); //1010
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to get the arrays of digits from a number in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to get the arrays of digits from a number only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const digitize = n => `${n}`.split('').map(v => parseInt(v, 10));
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const digitize = n => [...`${n}`].map(v => parseInt(v, 10));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
digitize(123); // [1, 2, 3]
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to multiply arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to multiply arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const mul = (...args) => args.reduce((a, b) => a * b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
mul(1, 2, 3, 4); // 24
|
||||
```
|
||||
@@ -0,0 +1,40 @@
|
||||
---
|
||||
title: How to prefix an integer with zeros in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to prefix an integer with zeros only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const prefixWithZeros = (number, length) => (number / Math.pow(10, length)).toFixed(length).substr(2);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const prefixWithZeros = (number, length) => `${Array(length).join('0')}${number}`.slice(-length);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const prefixWithZeros = (number, length) => String(number).padStart(length, '0');
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
prefixWithZeros(42, 5); // '00042'
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to round a number to a given number of digits in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to round a number to a given number of digits only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
round(1.234567, 3); // 1.235
|
||||
round(1.234567, 4); // 1.2346
|
||||
```
|
||||
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to subtract arguments in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to subtract arguments only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const subtract = (...args) => args.reduce((a, b) => a - b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
subtract(1, 2, 3, 4); // -8
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to truncate a number at decimal in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to truncate a number at decimal only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const truncate = n => ~~n;
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
truncate(25.198726354); // 25
|
||||
truncate(-25.198726354); // -25
|
||||
```
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: How to truncate a number to a given number of decimal places without rounding in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Number
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to truncate a number to a given number of decimal places without rounding only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const toFixed = (n, fixed) => `${n}`.match(new RegExp(`^-?\\d+(?:\.\\d{0,${fixed}})?`))[0];
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
toFixed(25.198726354, 1); // 25.1
|
||||
toFixed(25.198726354, 2); // 25.19
|
||||
toFixed(25.198726354, 3); // 25.198
|
||||
toFixed(25.198726354, 4); // 25.1987
|
||||
toFixed(25.198726354, 5); // 25.19872
|
||||
toFixed(25.198726354, 6); // 25.198726
|
||||
```
|
||||
Reference in new issue
Block a user