Update formatting and description of some older math snippets
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
---
|
||||
title: hammingDistance
|
||||
tags: math,beginner
|
||||
tags: math,regexp,intermediate
|
||||
---
|
||||
|
||||
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)`.
|
||||
Use the 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 `1`s in the string, using `match(/1/g)`.
|
||||
|
||||
```js
|
||||
|
||||
@ -10,7 +10,9 @@ Use `Math.round()` to convert to an integer.
|
||||
|
||||
```js
|
||||
const toSafeInteger = num =>
|
||||
Math.round(Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER));
|
||||
Math.round(
|
||||
Math.max(Math.min(num, Number.MAX_SAFE_INTEGER), Number.MIN_SAFE_INTEGER)
|
||||
);
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user