Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions logic-exercises/indexOf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
build
8 changes: 8 additions & 0 deletions logic-exercises/indexOf/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
roots: ["<rootDir>/tests"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
24 changes: 24 additions & 0 deletions logic-exercises/indexOf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "indexOf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"jest": "^27.4.7",
"ts-jest": "^27.1.3",
"ts-node-dev": "^1.1.8"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.14",
"typescript": "^4.5.5"
},
"scripts": {
"dev": "ts-node-dev --transpile-only --ignore-watch node_modules ./src/index.ts",
"test": "clear && jest",
"start": "tsc && node ./build/src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC"
}
22 changes: 22 additions & 0 deletions logic-exercises/indexOf/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const indexOf = (
fonte: string,
elementoDaPesquisa: string,
mainIndex: number = 0,
fonteIndex: number = 0,
elementoDaPesquisaIndex: number = 0
): number => {
//não existe
if (fonteIndex > fonte.length) {
return -1;
}

if (elementoDaPesquisaIndex >= elementoDaPesquisa.length) {
return mainIndex;
}

if (fonte[fonteIndex] === elementoDaPesquisa[elementoDaPesquisaIndex]) {
return indexOf(fonte, elementoDaPesquisa, mainIndex, fonteIndex + 1, elementoDaPesquisaIndex + 1);
} else {
return indexOf(fonte, elementoDaPesquisa, mainIndex + 1, mainIndex + 1, 0);
}
};
16 changes: 16 additions & 0 deletions logic-exercises/indexOf/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { indexOf } from "../src"

test("indexOfMétodo", ()=>{

const fonte = "abcdefghijklmnopqrstuvwxyz"
const elementoDaPesquisa1 = "c"
const elementoDaPesquisa2 = "2"

const result1 = indexOf(fonte,elementoDaPesquisa1)
const result2 = indexOf(fonte,elementoDaPesquisa2)

expect(result1).toBe(2);
expect(result2).toBe(-1);


})
13 changes: 13 additions & 0 deletions logic-exercises/indexOf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6" ,
"module": "commonjs",
"outDir": "./build" ,
"rootDir": "./" ,
"strict": true ,
"sourceMap": true,
"removeComments": true,
"esModuleInterop": true ,
"forceConsistentCasingInFileNames": true
}
}
Loading