From f23ed56967388568f8b41018c7cb5f3679f6d3bc Mon Sep 17 00:00:00 2001 From: 477885555 Date: Thu, 22 Sep 2022 15:18:14 -0300 Subject: [PATCH] recursividade --- .../recursividade/.codesandbox/workspace.json | 20 +++++++++++++ .../modulo2/MODULO3/recursividade/index.html | 15 ++++++++++ .../MODULO3/recursividade/package.json | 22 ++++++++++++++ .../src/checkEmptyObject.test.ts | 23 ++++++++++++++ .../recursividade/src/checkEmptyObject.ts | 30 +++++++++++++++++++ .../MODULO3/recursividade/src/factorial.ts | 10 +++++++ .../MODULO3/recursividade/src/fibonacci.ts | 7 +++++ .../MODULO3/recursividade/src/index.ts | 26 ++++++++++++++++ .../recursividade/src/printCharacters.ts | 10 +++++++ .../MODULO3/recursividade/tsconfig.json | 16 ++++++++++ 10 files changed, 179 insertions(+) create mode 100644 Back-end/modulo2/MODULO3/recursividade/.codesandbox/workspace.json create mode 100644 Back-end/modulo2/MODULO3/recursividade/index.html create mode 100644 Back-end/modulo2/MODULO3/recursividade/package.json create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.test.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/factorial.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/fibonacci.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/index.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/src/printCharacters.ts create mode 100644 Back-end/modulo2/MODULO3/recursividade/tsconfig.json diff --git a/Back-end/modulo2/MODULO3/recursividade/.codesandbox/workspace.json b/Back-end/modulo2/MODULO3/recursividade/.codesandbox/workspace.json new file mode 100644 index 0000000..e7d06a2 --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/.codesandbox/workspace.json @@ -0,0 +1,20 @@ +{ + "responsive-preview": { + "Mobile": [ + 320, + 675 + ], + "Tablet": [ + 1024, + 765 + ], + "Desktop": [ + 1400, + 800 + ], + "Desktop HD": [ + 1920, + 1080 + ] + } +} \ No newline at end of file diff --git a/Back-end/modulo2/MODULO3/recursividade/index.html b/Back-end/modulo2/MODULO3/recursividade/index.html new file mode 100644 index 0000000..9fcae6b --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/index.html @@ -0,0 +1,15 @@ + + + + Parcel Sandbox + + + + +
+ + + + + \ No newline at end of file diff --git a/Back-end/modulo2/MODULO3/recursividade/package.json b/Back-end/modulo2/MODULO3/recursividade/package.json new file mode 100644 index 0000000..a5f8eba --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/package.json @@ -0,0 +1,22 @@ +{ + "name": "recursividade", + "version": "1.0.0", + "description": "", + "main": "index.html", + "scripts": { + "start": "parcel index.html --open", + "build": "parcel build index.html" + }, + "dependencies": { + "@types/jest": "28.1.6", + "jest": "28.1.3", + "parcel-bundler": "^1.6.1" + }, + "devDependencies": { + "typescript": "4.4.4" + }, + "resolutions": { + "@babel/preset-env": "7.13.8" + }, + "keywords": [] +} \ No newline at end of file diff --git a/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.test.ts b/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.test.ts new file mode 100644 index 0000000..d57f7bd --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.test.ts @@ -0,0 +1,23 @@ +import { checkEmptyObject } from "./checkEmptyObject"; + +test("checkEmptyObject", () => { + expect(checkEmptyObject({ prop1: "maçã", prop2: "bananinha" })).toBe(false); + expect(checkEmptyObject({ prop1: undefined, prop2: "bananinha" })).toBe(true); + expect(checkEmptyObject({ prop1: null, prop2: "bananinha" })).toBe(true); + expect(checkEmptyObject({ prop1: 0, prop2: "bananinha" })).toBe(true); + expect(checkEmptyObject({ prop1: "", prop2: "bananinha" })).toBe(true); + expect(checkEmptyObject({ prop1: [], prop2: "bananinha" })).toBe(true); + expect(checkEmptyObject({ prop1: {}, prop2: "bananinha" })).toBe(true); + expect( + checkEmptyObject({ + prop1: { + outraProp: { + a: { + b: null + } + } + }, + prop2: "bananinha" + }) + ).toBe(true); +}); diff --git a/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.ts b/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.ts new file mode 100644 index 0000000..6949046 --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/checkEmptyObject.ts @@ -0,0 +1,30 @@ +function isEmpty(value: any): boolean { + return ( + value === undefined || + value === null || + value === 0 || + value === "" || + (Array.isArray(value) && value.length === 0) || + (typeof value === "object" && + (Object.values(value!).length === 0 || checkEmptyObject(value))) + ); +} + +export function checkEmptyObject(obj: any): boolean { + // exemplo: + // const obj = { + // name: "nome", + // id: "Id", + // number: 5 + // } + + const values = Object.values(obj); + // exemplo array values + // ["nome", "id", 5] + for (let value of values) { + if (isEmpty(value)) { + return true; + } + } + return false; +} diff --git a/Back-end/modulo2/MODULO3/recursividade/src/factorial.ts b/Back-end/modulo2/MODULO3/recursividade/src/factorial.ts new file mode 100644 index 0000000..d484a02 --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/factorial.ts @@ -0,0 +1,10 @@ +export const factorial = (n: number): number => { + if (n <= 0) { + return 1; + } + + return n * factorial(n - 1); + // exmplo com n=2 => 2* 1 *1 + // exmplo com n=3 => 3* 2* 1 *1 + // exmplo com n=4 => 4* 3* 2* 1 *1 +}; diff --git a/Back-end/modulo2/MODULO3/recursividade/src/fibonacci.ts b/Back-end/modulo2/MODULO3/recursividade/src/fibonacci.ts new file mode 100644 index 0000000..a2c6351 --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/fibonacci.ts @@ -0,0 +1,7 @@ +export const fibonacci = (n: number): number => { + if (n <= 2) { + return 1; + } + + return fibonacci(n - 1) + fibonacci(n - 2); +}; diff --git a/Back-end/modulo2/MODULO3/recursividade/src/index.ts b/Back-end/modulo2/MODULO3/recursividade/src/index.ts new file mode 100644 index 0000000..07fe68e --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/index.ts @@ -0,0 +1,26 @@ +import { fibonacci } from "./fibonacci"; +import { factorial } from "./factorial"; +import { printCharacters } from "./printCharacters"; +import { checkEmptyObject } from "./checkEmptyObject"; + +// console.log("factorial:"); +// console.log("1", factorial(1)); +// console.log("2", factorial(2)); +// console.log("3", factorial(3)); +// console.log("4", factorial(4)); +// console.log("5", factorial(5)); +// console.log("6", factorial(6)); + +//console.log("fibonacci:"); +// console.log("1", fibonacci(1)); +// console.log("2", fibonacci(2)); +// console.log("3", fibonacci(3)); +// console.log("4", fibonacci(4)); +// console.log("5", fibonacci(5)); +// console.log("6", fibonacci(6)); +//console.log("Count", fibonacci(7)); + +// console.log("printCharacters:"); +// console.log("Olá", printCharacters("Olá")); +// console.log("tudo", printCharacters("tudo")); +// console.log("bem?", printCharacters("bem?")); diff --git a/Back-end/modulo2/MODULO3/recursividade/src/printCharacters.ts b/Back-end/modulo2/MODULO3/recursividade/src/printCharacters.ts new file mode 100644 index 0000000..02720bc --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/src/printCharacters.ts @@ -0,0 +1,10 @@ +export const printCharacters = (str: string, index: number = 0): void => { + // if (index < str.length) { + // console.log(str[index]); + // printCharacters(str, index + 1); + // } + + for (let char of str) { + console.log(char); + } +}; diff --git a/Back-end/modulo2/MODULO3/recursividade/tsconfig.json b/Back-end/modulo2/MODULO3/recursividade/tsconfig.json new file mode 100644 index 0000000..ef02d40 --- /dev/null +++ b/Back-end/modulo2/MODULO3/recursividade/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "strict": true, + "module": "commonjs", + "jsx": "preserve", + "esModuleInterop": true, + "sourceMap": true, + "allowJs": true, + "lib": [ + "es2017", + "dom" + ], + "rootDir": "src", + "moduleResolution": "node" + } +} \ No newline at end of file