diff --git a/03_recursion/ts/01_countdown.ts b/03_recursion/ts/01_countdown.ts new file mode 100644 index 0000000..b4463df --- /dev/null +++ b/03_recursion/ts/01_countdown.ts @@ -0,0 +1,13 @@ +function countdown(i: number): null { + console.log(i); + + if (i <= 0) { + return null; + } + + countdown(i-1); + + return null; +} + +console.log(countdown(5)); diff --git a/03_recursion/ts/02_greet.ts b/03_recursion/ts/02_greet.ts new file mode 100644 index 0000000..10e1197 --- /dev/null +++ b/03_recursion/ts/02_greet.ts @@ -0,0 +1,16 @@ +function greet2(name: string): void { + console.log('how are you, ' + name + '?'); +} + +function bye(): void { + console.log('ok bye!'); +} + +function greet(name: string): void { + console.log('hello, ' + name + '!'); + greet2(name); + console.log('getting ready to say bye...'); + bye(); +} + +greet('adit'); diff --git a/03_recursion/ts/03_factorial.ts b/03_recursion/ts/03_factorial.ts new file mode 100644 index 0000000..9693d59 --- /dev/null +++ b/03_recursion/ts/03_factorial.ts @@ -0,0 +1,9 @@ +function fact(x: number): number { + if (x === 1) { + return x; + } + + return x * fact(x - 1); +} + +console.log(fact(5)); \ No newline at end of file diff --git a/04_quicksort/ts/01_loop_sum.ts b/04_quicksort/ts/01_loop_sum.ts new file mode 100644 index 0000000..91b30ca --- /dev/null +++ b/04_quicksort/ts/01_loop_sum.ts @@ -0,0 +1,10 @@ +function sum(arr: number[]): number { + let total = 0; + arr.forEach(number => { + total += number; + }); + + return total; +} + +console.log(sum([1, 2, 3, 4])); \ No newline at end of file diff --git a/04_quicksort/ts/02_recursive_sum.ts b/04_quicksort/ts/02_recursive_sum.ts new file mode 100644 index 0000000..6fde8c8 --- /dev/null +++ b/04_quicksort/ts/02_recursive_sum.ts @@ -0,0 +1,9 @@ +function sum(arr: number[]): number { + if (arr.length === 1) { + return arr[0]; + } + + return arr[0] + sum(arr.slice(1)); +} + +console.log(sum([1, 2, 3, 4])); \ No newline at end of file diff --git a/04_quicksort/ts/03_recursive_count.ts b/04_quicksort/ts/03_recursive_count.ts new file mode 100644 index 0000000..08c6827 --- /dev/null +++ b/04_quicksort/ts/03_recursive_count.ts @@ -0,0 +1,9 @@ +function count(list: T[]): number { + if (list.length === 1) { + return 1; + } + + return 1 + count(list.slice(1)); +} + +console.log(count([1, 2, 3])); \ No newline at end of file diff --git a/04_quicksort/ts/04_recursive_max.ts b/04_quicksort/ts/04_recursive_max.ts new file mode 100644 index 0000000..88aee89 --- /dev/null +++ b/04_quicksort/ts/04_recursive_max.ts @@ -0,0 +1,15 @@ +function max(arr: number[]): number | null { + if (arr.length === 0) { + return null; + } + + if (arr.length === 1) { + return arr[0]; + } + + const subMax = max(arr.slice(1)); + + return arr[0] > subMax ? arr[0] : subMax; +}; + +console.log(max([1, 2, 4, 3])); \ No newline at end of file diff --git a/04_quicksort/ts/05_quicksort.ts b/04_quicksort/ts/05_quicksort.ts new file mode 100644 index 0000000..c9223cb --- /dev/null +++ b/04_quicksort/ts/05_quicksort.ts @@ -0,0 +1,13 @@ +function quicksort(arr: number[]): number[] { + if (arr.length < 2) { + return arr; + } + + const pivot = arr[0]; + const less = arr.slice(1).filter(el => el <= pivot); + const greater = arr.slice(1).filter(el => el > pivot); + + return quicksort(less).concat(pivot, quicksort(greater)); +} + +console.log(quicksort([10, 5, 2, 3]));