diff --git a/modulo6/recursividade/.gitignore b/modulo6/recursividade/.gitignore new file mode 100644 index 0000000..d1cb904 --- /dev/null +++ b/modulo6/recursividade/.gitignore @@ -0,0 +1,3 @@ +node_modules +package-lock.json +build diff --git a/modulo6/recursividade/package.json b/modulo6/recursividade/package.json new file mode 100644 index 0000000..06d6393 --- /dev/null +++ b/modulo6/recursividade/package.json @@ -0,0 +1,13 @@ +{ + "scripts": { + "exercicio1": "tsc && node ./build/Exercicio1/index.js", + "exercicio2": "tsc && node ./build/Exercicio2/index.js", + "exercicio3": "tsc && node ./build/Exercicio3/index.js", + "exercicio4": "tsc && node ./build/Exercicio4/index.js" + }, + "devDependencies": { + "@types/node": "^15.3.0", + "ts-node-dev": "^1.1.6", + "typescript": "^4.2.4" + } +} diff --git a/modulo6/recursividade/src/Exercicio1/index.ts b/modulo6/recursividade/src/Exercicio1/index.ts new file mode 100644 index 0000000..976c4e3 --- /dev/null +++ b/modulo6/recursividade/src/Exercicio1/index.ts @@ -0,0 +1,18 @@ +//a + +const printNumbers = (n: number) => { + if (n >= 0) { + printNumbers(n - 1); + console.log(n); + } + }; + + + //b + + const printNumbers2 = (n: number) => { + if (n >= 0) { + console.log(n); + printNumbers2(n - 1); + } + }; \ No newline at end of file diff --git a/modulo6/recursividade/src/Exercicio2/index.ts b/modulo6/recursividade/src/Exercicio2/index.ts new file mode 100644 index 0000000..91c7962 --- /dev/null +++ b/modulo6/recursividade/src/Exercicio2/index.ts @@ -0,0 +1,12 @@ +export const calculateSumTo = (n: number, acc: number = 0): number => { + if (n === 0) { + return acc; + } + return calculateSumTo(n - 1, acc + n); + }; + + + // Exemplos de uso: + console.log(calculateSumTo(3)); + console.log(calculateSumTo(10)); + console.log(calculateSumTo(100)); \ No newline at end of file diff --git a/modulo6/recursividade/src/Exercicio3/index.ts b/modulo6/recursividade/src/Exercicio3/index.ts new file mode 100644 index 0000000..32f0cc7 --- /dev/null +++ b/modulo6/recursividade/src/Exercicio3/index.ts @@ -0,0 +1,13 @@ +export const calculateSumTo = (n: number): number => { + var sum = 0 + for (var i = 0 ; i <= n ; i++) { + sum += i; + } + return sum; + }; + + // Exemplos de uso: + console.log(calculateSumTo(3)); + console.log(calculateSumTo(10)); + console.log(calculateSumTo(100)); + diff --git a/modulo6/recursividade/src/Exercicio4/index.ts b/modulo6/recursividade/src/Exercicio4/index.ts new file mode 100644 index 0000000..60f3aeb --- /dev/null +++ b/modulo6/recursividade/src/Exercicio4/index.ts @@ -0,0 +1,10 @@ +export const printArray = (arr: number[], i: number = arr.length - 1) => { + if (i >= 0) { + printArray(arr, i - 1); + console.log(`Elemento ${i}: `, arr[i]); + } + }; + + + const array = [1, 2, 3, 4]; + printArray(array); \ No newline at end of file diff --git a/modulo6/recursividade/tsconfig.json b/modulo6/recursividade/tsconfig.json new file mode 100644 index 0000000..f9701d4 --- /dev/null +++ b/modulo6/recursividade/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "sourceMap": true, + "outDir": "./build", + "rootDir": "./src", + "removeComments": true, + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + } +} \ No newline at end of file