From bfedc2df649e932c80a34fa260b24b0ea0cda1d2 Mon Sep 17 00:00:00 2001 From: adam smith Date: Thu, 12 Oct 2023 09:38:46 +0100 Subject: [PATCH 1/4] completed 04-02 --- 04-recursion/02-unwinding/readme.md | 0 04-recursion/02-unwinding/sum-up-to-run.js | 0 04-recursion/02-unwinding/sum-up-to-solution.js | 0 04-recursion/02-unwinding/sum-up-to.js | 10 +++++++++- .../{sum-up-to-test.js => sum-up-to.test.js} | 2 ++ 5 files changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 04-recursion/02-unwinding/readme.md mode change 100644 => 100755 04-recursion/02-unwinding/sum-up-to-run.js mode change 100644 => 100755 04-recursion/02-unwinding/sum-up-to-solution.js mode change 100644 => 100755 04-recursion/02-unwinding/sum-up-to.js rename 04-recursion/02-unwinding/{sum-up-to-test.js => sum-up-to.test.js} (80%) mode change 100644 => 100755 diff --git a/04-recursion/02-unwinding/readme.md b/04-recursion/02-unwinding/readme.md old mode 100644 new mode 100755 diff --git a/04-recursion/02-unwinding/sum-up-to-run.js b/04-recursion/02-unwinding/sum-up-to-run.js old mode 100644 new mode 100755 diff --git a/04-recursion/02-unwinding/sum-up-to-solution.js b/04-recursion/02-unwinding/sum-up-to-solution.js old mode 100644 new mode 100755 diff --git a/04-recursion/02-unwinding/sum-up-to.js b/04-recursion/02-unwinding/sum-up-to.js old mode 100644 new mode 100755 index 24b8537e..1ceb1243 --- a/04-recursion/02-unwinding/sum-up-to.js +++ b/04-recursion/02-unwinding/sum-up-to.js @@ -1,3 +1,11 @@ -function sumUpTo() {} +function sumUpTo(n) { + if (n === 0) return 0; + + if (n === 1) { + return 1; + } + + return n + sumUpTo(n - 1); +} module.exports = sumUpTo; diff --git a/04-recursion/02-unwinding/sum-up-to-test.js b/04-recursion/02-unwinding/sum-up-to.test.js old mode 100644 new mode 100755 similarity index 80% rename from 04-recursion/02-unwinding/sum-up-to-test.js rename to 04-recursion/02-unwinding/sum-up-to.test.js index 0df1a4ec..deb4e136 --- a/04-recursion/02-unwinding/sum-up-to-test.js +++ b/04-recursion/02-unwinding/sum-up-to.test.js @@ -1,3 +1,5 @@ +const sumUpTo = require('./sum-up-to'); + test('Summing up positive integers', () => { expect(sumUpTo(5)).toBe(15); expect(sumUpTo(10)).toBe(55); From ad3bcb06579ebbcff68c8d85795afe5b0912b445 Mon Sep 17 00:00:00 2001 From: adam smith Date: Thu, 12 Oct 2023 09:49:02 +0100 Subject: [PATCH 2/4] fixed access right --- 04-recursion/02-unwinding/readme.md | 0 04-recursion/02-unwinding/sum-up-to-run.js | 0 04-recursion/02-unwinding/sum-up-to-solution.js | 0 04-recursion/02-unwinding/sum-up-to.js | 0 04-recursion/02-unwinding/sum-up-to.test.js | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 04-recursion/02-unwinding/readme.md mode change 100755 => 100644 04-recursion/02-unwinding/sum-up-to-run.js mode change 100755 => 100644 04-recursion/02-unwinding/sum-up-to-solution.js mode change 100755 => 100644 04-recursion/02-unwinding/sum-up-to.js mode change 100755 => 100644 04-recursion/02-unwinding/sum-up-to.test.js diff --git a/04-recursion/02-unwinding/readme.md b/04-recursion/02-unwinding/readme.md old mode 100755 new mode 100644 diff --git a/04-recursion/02-unwinding/sum-up-to-run.js b/04-recursion/02-unwinding/sum-up-to-run.js old mode 100755 new mode 100644 diff --git a/04-recursion/02-unwinding/sum-up-to-solution.js b/04-recursion/02-unwinding/sum-up-to-solution.js old mode 100755 new mode 100644 diff --git a/04-recursion/02-unwinding/sum-up-to.js b/04-recursion/02-unwinding/sum-up-to.js old mode 100755 new mode 100644 diff --git a/04-recursion/02-unwinding/sum-up-to.test.js b/04-recursion/02-unwinding/sum-up-to.test.js old mode 100755 new mode 100644 From d802812b1641c69592b964713a8fbd74af52c793 Mon Sep 17 00:00:00 2001 From: adam smith Date: Thu, 12 Oct 2023 09:59:19 +0100 Subject: [PATCH 3/4] improved fix and added notes to readme --- 04-recursion/02-unwinding/readme.md | 8 ++++---- 04-recursion/02-unwinding/sum-up-to-solution.js | 5 +++-- 04-recursion/02-unwinding/sum-up-to.js | 10 +--------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/04-recursion/02-unwinding/readme.md b/04-recursion/02-unwinding/readme.md index 785f75f4..0c3e46ed 100644 --- a/04-recursion/02-unwinding/readme.md +++ b/04-recursion/02-unwinding/readme.md @@ -10,8 +10,8 @@ Let's look at an example of this. Consider the following function: ```js function sumUpTo(n) { - if (n === 1) { - return 1; + if (n <= 1) { + return n; } return n + sumUpTo(n - 1); @@ -50,9 +50,9 @@ We can see this by logging the function calls and the return values: ```js function sumUpTo(n) { - if (n === 1) { + if (n <= 1) { console.log(`sumUpTo(${n}) returns 1`); - return 1; + return n; } console.log(`sumUpTo(${n}) calls sumUpTo(${n - 1}) + ${n}`); diff --git a/04-recursion/02-unwinding/sum-up-to-solution.js b/04-recursion/02-unwinding/sum-up-to-solution.js index 45b5ba60..0366e01e 100644 --- a/04-recursion/02-unwinding/sum-up-to-solution.js +++ b/04-recursion/02-unwinding/sum-up-to-solution.js @@ -1,7 +1,8 @@ function sumUpTo(n) { // Base case - when n is 1, we return 1 - if (n === 1) { - return 1; + // if n is 0 or negative, we handle that case otherwise infinite recursion + if (n <= 1) { + return n; } // Recursive case - when n is greater than 1, we return the sum of n and sumUpTo(n - 1) diff --git a/04-recursion/02-unwinding/sum-up-to.js b/04-recursion/02-unwinding/sum-up-to.js index 1ceb1243..24b8537e 100644 --- a/04-recursion/02-unwinding/sum-up-to.js +++ b/04-recursion/02-unwinding/sum-up-to.js @@ -1,11 +1,3 @@ -function sumUpTo(n) { - if (n === 0) return 0; - - if (n === 1) { - return 1; - } - - return n + sumUpTo(n - 1); -} +function sumUpTo() {} module.exports = sumUpTo; From 668a588735ce46a335c6c5ef2cd82c50105d98b4 Mon Sep 17 00:00:00 2001 From: adam smith Date: Thu, 12 Oct 2023 10:06:46 +0100 Subject: [PATCH 4/4] changed file name back to -test instead of .test --- .../02-unwinding/{sum-up-to.test.js => sum-up-to-test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 04-recursion/02-unwinding/{sum-up-to.test.js => sum-up-to-test.js} (100%) diff --git a/04-recursion/02-unwinding/sum-up-to.test.js b/04-recursion/02-unwinding/sum-up-to-test.js similarity index 100% rename from 04-recursion/02-unwinding/sum-up-to.test.js rename to 04-recursion/02-unwinding/sum-up-to-test.js