Update formatting
This commit is contained in:
@ -7,8 +7,8 @@ lastUpdated: 2020-11-03T22:11:18+02:00
|
||||
|
||||
Converts the values of RGB components to a hexadecimal color code.
|
||||
|
||||
- Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `Number.prototype.toString(16)`.
|
||||
- Use `String.prototype.padStart(6, '0')` to get a 6-digit hexadecimal value.
|
||||
- Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `Number.prototype.toString()`.
|
||||
- Use `String.prototype.padStart()` to get a 6-digit hexadecimal value.
|
||||
|
||||
```js
|
||||
const RGBToHex = (r, g, b) =>
|
||||
|
||||
@ -8,7 +8,7 @@ lastUpdated: 2020-10-22T20:24:44+03:00
|
||||
Generates a UUID in a browser.
|
||||
|
||||
- Use `Crypto.getRandomValues()` to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
|
||||
- Use `Number.prototype.toString(16)` to convert it to a proper UUID.
|
||||
- Use `Number.prototype.toString()` to convert it to a proper UUID (hexadecimal string).
|
||||
|
||||
```js
|
||||
const UUIDGeneratorBrowser = () =>
|
||||
|
||||
@ -8,7 +8,7 @@ lastUpdated: 2020-10-22T20:24:44+03:00
|
||||
Generates a UUID in Node.JS.
|
||||
|
||||
- Use `crypto.randomBytes()` to generate a UUID, compliant with [RFC4122](https://www.ietf.org/rfc/rfc4122.txt) version 4.
|
||||
- Use `Number.prototype.toString(16)` to convert it to a proper UUID.
|
||||
- Use `Number.prototype.toString()` to convert it to a proper UUID (hexadecimal string).
|
||||
|
||||
```js
|
||||
const crypto = require('crypto');
|
||||
|
||||
@ -10,7 +10,7 @@ Only works as a result of user action (i.e. inside a `click` event listener).
|
||||
|
||||
- Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
|
||||
- Use `Selection.getRangeAt()`to store the selected range (if any).
|
||||
- Use `Document.execCommand('copy')` to copy to the clipboard.
|
||||
- Use `Document.execCommand()` to copy to the clipboard.
|
||||
- Remove the `<textarea>` element from the HTML document.
|
||||
- Finally, use `Selection.addRange()` to recover the original selected range (if any).
|
||||
- **Note:** You can use the asynchronous [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) in most current browsers. You can find out more about it in the [copyToClipboardAsync snippet](/js/s/copy-to-clipboard-async).
|
||||
|
||||
@ -8,8 +8,8 @@ lastUpdated: 2020-12-28T13:49:24+02:00
|
||||
Calculates the Hamming distance between two values.
|
||||
|
||||
- Use the XOR operator (`^`) to find the bit difference between the two numbers.
|
||||
- Convert to a binary string using `Number.prototype.toString(2)`.
|
||||
- Count and return the number of `1`s in the string, using `String.prototype.match(/1/g)`.
|
||||
- Convert to a binary string using `Number.prototype.toString()`.
|
||||
- Count and return the number of `1`s in the string, using `String.prototype.match()`.
|
||||
|
||||
```js
|
||||
const hammingDistance = (num1, num2) =>
|
||||
|
||||
@ -11,7 +11,7 @@ Returns a promise.
|
||||
- Use the [SubtleCrypto](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) API to create a hash for the given value.
|
||||
- Create a new `TextEncoder` and use it to encode `val`. Pass its value to `SubtleCrypto.digest()` to generate a digest of the given data.
|
||||
- Use `DataView.prototype.getUint32()` to read data from the resolved `ArrayBuffer`.
|
||||
- Convert the data to it hexadecimal representation using `Number.prototype.toString(16)`. Add the data to an array using `Array.prototype.push()`.
|
||||
- Convert the data to its hexadecimal representation using `Number.prototype.toString()`. Add the data to an array using `Array.prototype.push()`.
|
||||
- Finally, use `Array.prototype.join()` to combine values in the array of `hexes` into a string.
|
||||
|
||||
```js
|
||||
|
||||
@ -8,8 +8,9 @@ lastUpdated: 2020-10-22T20:24:04+03:00
|
||||
Generates a random string with the specified length.
|
||||
|
||||
- Use `Array.from()` to create a new array with the specified `length`.
|
||||
- Use `Math.random()` generate a random floating-point number, `Number.prototype.toString(36)` to convert it to an alphanumeric string.
|
||||
- Use `String.prototype.slice(2)` to remove the integral part and decimal point from each generated number.
|
||||
- Use `Math.random()` generate a random floating-point number.
|
||||
- Use `Number.prototype.toString()` with a `radix` value of `36` to convert it to an alphanumeric string.
|
||||
- Use `String.prototype.slice()` to remove the integral part and decimal point from each generated number.
|
||||
- Use `Array.prototype.some()` to repeat this process as many times as required, up to `length`, as it produces a variable-length string each time.
|
||||
- Finally, use `String.prototype.slice()` to trim down the generated string if it's longer than the given `length`.
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ lastUpdated: 2021-01-08T00:23:44+02:00
|
||||
Generates a random hexadecimal color code.
|
||||
|
||||
- Use `Math.random()` to generate a random 24-bit (6 * 4bits) hexadecimal number.
|
||||
- Use bit shifting and then convert it to an hexadecimal string using `Number.prototype.toString(16)`.
|
||||
- Use bit shifting and then convert it to an hexadecimal string using `Number.prototype.toString()`.
|
||||
|
||||
```js
|
||||
const randomHexColorCode = () => {
|
||||
|
||||
Reference in New Issue
Block a user