From 6f8ed6d1b9355508d40b6fb1e7dfd8f4c2522954 Mon Sep 17 00:00:00 2001 From: Denys Kozachok Date: Tue, 18 Feb 2025 13:21:40 -0800 Subject: [PATCH] Lesson 7: type script --- package.json | 2 +- src/lesson7/arrow-functions.ts | 11 +++++++++++ src/lesson7/functions.ts | 16 ++++++++++++++++ tsconfig.json | 7 ++++--- 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/lesson7/arrow-functions.ts create mode 100644 src/lesson7/functions.ts diff --git a/package.json b/package.json index 8fc014a..dafff0f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/index.js", "scripts": { "lint": "eslint src/", - "build": "npm run lint" + "build": "tsc" }, "devDependencies": { "@eslint/js": "^9.19.0", diff --git a/src/lesson7/arrow-functions.ts b/src/lesson7/arrow-functions.ts new file mode 100644 index 0000000..9516056 --- /dev/null +++ b/src/lesson7/arrow-functions.ts @@ -0,0 +1,11 @@ +const sumArrayArrow = (arr: (number | string)[]): number => { + return arr.reduce((acc, item) => acc + Number(item), 0); +}; + +// Приклади масивів +const numberArray2: number[] = [1, 2, 3, 4]; +const stringArray2: string[] = ["10", "20", "30"]; + +// Виклики стрілочної функції та виведення результатів +console.log("Sum of numberArray2 =", sumArrayArrow(numberArray2)); // 10 +console.log("Sum of stringArray2 =", sumArrayArrow(stringArray2)); // 60 diff --git a/src/lesson7/functions.ts b/src/lesson7/functions.ts new file mode 100644 index 0000000..590fe89 --- /dev/null +++ b/src/lesson7/functions.ts @@ -0,0 +1,16 @@ +function sumArray(arr: (number | string)[]): number { + let total = 0; + for (const item of arr) { + // Перетворюємо кожен елемент у число + total += typeof item === "number" ? item : Number(item); + } + return total; +} + +// Приклади масивів +const numberArray: number[] = [10, 20, 30]; +const stringArray: string[] = ["5", "15", "25"]; + +// Виклики функції та виведення результатів у консоль +console.log("Sum of numberArray =", sumArray(numberArray)); // 60 +console.log("Sum of stringArray =", sumArray(stringArray)); // 45 diff --git a/tsconfig.json b/tsconfig.json index 1efdceb..e8c39f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,11 +2,12 @@ "compilerOptions": { "target": "es2016", "module": "commonjs", + "rootDir": "src", + "outDir": "dist", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, - "skipLibCheck": true, - "outDir": "dist" + "skipLibCheck": true }, "include": ["src"] -} +} \ No newline at end of file