From 1b5b6d5deb5da919e17c02d078dc40ca8de7d107 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 5 Mar 2020 23:06:48 +0200 Subject: [PATCH] Update formatting and description of some older math snippets --- snippets/hammingDistance.md | 6 +++--- snippets/toSafeInteger.md | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/snippets/hammingDistance.md b/snippets/hammingDistance.md index f95e80d90..0f5e36de9 100644 --- a/snippets/hammingDistance.md +++ b/snippets/hammingDistance.md @@ -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 @@ -14,4 +14,4 @@ const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) | ```js hammingDistance(2, 3); // 1 -``` \ No newline at end of file +``` diff --git a/snippets/toSafeInteger.md b/snippets/toSafeInteger.md index 602517cb5..7588a2dbe 100644 --- a/snippets/toSafeInteger.md +++ b/snippets/toSafeInteger.md @@ -10,10 +10,12 @@ 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 toSafeInteger('3.2'); // 3 toSafeInteger(Infinity); // 9007199254740991 -``` \ No newline at end of file +```