We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d37ff7f commit 9462862Copy full SHA for 9462862
Algorithms/Recursion/Basics.ts
@@ -0,0 +1,30 @@
1
+function inception(repeat: number): string {
2
+ // 1. Base Case(s)
3
+ if (repeat === 0) return 'Done!\n'; // Base Case(s) must be written first in method
4
+ // 2. Input Validation
5
+ if (repeat < 1) return 'Too small!\n'; // Input validation must occur after base case(s)
6
+
7
+ console.log('Counter:', repeat);
8
9
+ // 3. Recursive Call
10
+ return inception(repeat-1); // The recursive call should return itself so the calculated value can bubble up
11
+}
12
13
+console.log(inception(5));
14
+console.log(inception(1));
15
+console.log(inception(-1));
16
17
+// RUN: deno run Algorithms/Recursion/Basics.ts
18
19
+// --------------------------- Terminal Output: ---------------------------
20
+// Counter: 5
21
+// Counter: 4
22
+// Counter: 3
23
+// Counter: 2
24
+// Counter: 1
25
+// Done!
26
+//
27
28
29
30
+// Too small!
Algorithms/Recursion/Factorial.ts
Algorithms/Recursion/Fibonacci.ts
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments