From 9339e9925a0d21e22bed65dc3d3a66fb7e7f3ca1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?=
 <76864299+Rudxain@users.noreply.github.com>
Date: Fri, 16 Sep 2022 03:47:00 -0400
Subject: [PATCH 1/2] Update collatz.js

Better docs, refactoring, support for negatives, protection against infinite loops, etc...
---
 algorithms/math/collatz.js | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/algorithms/math/collatz.js b/algorithms/math/collatz.js
index aebbe70..c7fb9c4 100644
--- a/algorithms/math/collatz.js
+++ b/algorithms/math/collatz.js
@@ -1,31 +1,36 @@
-/* Collatz conjecture */
+"use strict";
 
-function collatz(n) {
-    var numbers = []
+/**known Collatz infinite loops*/
+const CYCLES = new Set([
+    1, 0, -1, -5, -17, NaN, Infinity, -Infinity
+]);
 
-    while (n > 1) {
-        numbers.push(n)
-        if (n % 2 === 0) {
-            n = n / 2;
-        } else {
-            n = (3 * n) + 1;
-        }
+/**
+ * Collatz conjecture
+ * @param {number} n
+*/
+const collatz = n => {
+    const numbers = [];
+
+    while ( !CYCLES.has(n) ) {
+        numbers.push(n);
+        n = n % 2 === 0 ? n / 2 : 3 * n + 1;
     }
-    numbers.push(n)
-    return numbers
+    numbers.push(n);
+    return numbers;
 }
 
 console.log(
-    'Collatz conjecture for n = 11',
+    'Hailstone sequence of 11',
     collatz(11)
 )
 
 console.log(
-    'Collatz conjecture for n = 27',
+    'Hailstone sequence of 27',
     collatz(27)
 )
 
 console.log(
-    'Collatz conjecture for n = 51',
+    'Hailstone sequence of 51',
     collatz(51)
 )

From 45cee63afc826f0e63d588011b3f515502a00299 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?=
 <76864299+Rudxain@users.noreply.github.com>
Date: Sat, 17 Sep 2022 11:04:47 -0400
Subject: [PATCH 2/2] Update `collatz.js`

---
 algorithms/math/collatz.js | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/algorithms/math/collatz.js b/algorithms/math/collatz.js
index c7fb9c4..f7335e7 100644
--- a/algorithms/math/collatz.js
+++ b/algorithms/math/collatz.js
@@ -6,31 +6,22 @@ const CYCLES = new Set([
 ]);
 
 /**
- * Collatz conjecture
- * @param {number} n
+ * Collatz conjecture calculator.
+ * returns an array containing the Hailstone sequence of `n`.
+ * @param {number} n "seed"
 */
 const collatz = n => {
-    const numbers = [];
+    const sequence = [];
 
     while ( !CYCLES.has(n) ) {
-        numbers.push(n);
+        sequence.push(n);
         n = n % 2 === 0 ? n / 2 : 3 * n + 1;
     }
-    numbers.push(n);
-    return numbers;
+    sequence.push(n);
+    return sequence;
 }
 
-console.log(
-    'Hailstone sequence of 11',
-    collatz(11)
-)
-
-console.log(
-    'Hailstone sequence of 27',
-    collatz(27)
-)
-
-console.log(
-    'Hailstone sequence of 51',
-    collatz(51)
-)
+[11, 27, 51, -7].forEach(n => console.log(
+    `Hailstone sequence of ${n}`,
+    collatz(n)
+));
\ No newline at end of file