From dc96a70e5c05da0c56304841517649f5f5511fca Mon Sep 17 00:00:00 2001 From: kotleta96 Date: Thu, 24 Oct 2024 12:46:22 +0300 Subject: [PATCH 1/3] codewars --- codewars/Adding Big Numbers/add.js | 14 +++++++ codewars/Anagram difference/anagram.js | 30 +++++++++++++ codewars/Array Deep Count/deepcount.js | 11 +++++ codewars/Build Tower/buildtower.js | 12 ++++++ .../Convert string to camel case/camelcase.js | 15 +++++++ codewars/Duplicate Encoder/dublicate.js | 13 ++++++ .../Find the missing letter/missingletter.js | 9 ++++ codewars/Flatten a Nested Map/map.js | 24 +++++++++++ codewars/Fun with tree - max sum/.gitkeep | 1 - codewars/Fun with tree - max sum/tree.js | 18 ++++++++ .../linkedlist.js | 19 +++++++++ codewars/Merge two arrays/merge2arrays.js | 12 ++++++ .../Moving Zeros To The End/movingzeros.js | 14 +++++++ codewars/Permutations/permutations.js | 18 ++++++++ .../productfib.js | 10 +++++ codewars/Simple Pig Latin/piglatin.js | 22 ++++++++++ codewars/Snail/snail.js | 36 ++++++++++++++++ .../sumofdigits.js | 14 +++++++ codewars/Sum of Intervals/sumofintervals.js | 22 ++++++++++ codewars/Sum of pairs/sumofpairs,js | 11 +++++ codewars/Tic-Tac-Toe Checker/tictac.js | 42 +++++++++++++++++++ codewars/Valid Parentheses/valid.js | 18 ++++++++ codewars/Where my anagrams at/anagrams.js | 5 +++ rpgsaga/.vscode/launch.json | 4 +- rpgsaga/saga/package-lock.json | 19 +++++---- rpgsaga/saga/package.json | 7 ++-- 26 files changed, 404 insertions(+), 16 deletions(-) create mode 100644 codewars/Adding Big Numbers/add.js create mode 100644 codewars/Anagram difference/anagram.js create mode 100644 codewars/Array Deep Count/deepcount.js create mode 100644 codewars/Build Tower/buildtower.js create mode 100644 codewars/Convert string to camel case/camelcase.js create mode 100644 codewars/Duplicate Encoder/dublicate.js create mode 100644 codewars/Find the missing letter/missingletter.js create mode 100644 codewars/Flatten a Nested Map/map.js create mode 100644 codewars/Fun with tree - max sum/tree.js create mode 100644 codewars/Linked Lists - Sorted Insert/linkedlist.js create mode 100644 codewars/Merge two arrays/merge2arrays.js create mode 100644 codewars/Moving Zeros To The End/movingzeros.js create mode 100644 codewars/Permutations/permutations.js create mode 100644 codewars/Product of consecutive Fib numbers/productfib.js create mode 100644 codewars/Simple Pig Latin/piglatin.js create mode 100644 codewars/Snail/snail.js create mode 100644 codewars/Sum of Digits - Digital Root/sumofdigits.js create mode 100644 codewars/Sum of Intervals/sumofintervals.js create mode 100644 codewars/Sum of pairs/sumofpairs,js create mode 100644 codewars/Tic-Tac-Toe Checker/tictac.js create mode 100644 codewars/Valid Parentheses/valid.js create mode 100644 codewars/Where my anagrams at/anagrams.js diff --git a/codewars/Adding Big Numbers/add.js b/codewars/Adding Big Numbers/add.js new file mode 100644 index 000000000..b786843f6 --- /dev/null +++ b/codewars/Adding Big Numbers/add.js @@ -0,0 +1,14 @@ +function add(a, b) { + let result = ""; + let value = 0; + a = a.split(""); + b = b.split(""); + + while (a.length || b.length || value) { + value += ~~a.pop() + ~~b.pop(); + result = (value % 10) + result; + value = value > 9; + } + + return result; + } \ No newline at end of file diff --git a/codewars/Anagram difference/anagram.js b/codewars/Anagram difference/anagram.js new file mode 100644 index 000000000..2a0ba7c51 --- /dev/null +++ b/codewars/Anagram difference/anagram.js @@ -0,0 +1,30 @@ +function anagramDifference(first, second) { + if (first === "" && second === "") { + return 0; + } else if (first === "" || second === "") { + if (first === "") { + return second.length; + } else { + return first.length; + } + } + let сount = {}; + if (first !== "" && second !== "") { + for (let letter of first) { + сount[letter] = (сount[letter] ?? 0) + 1; + } + + for (let letter of second) { + if (сount[letter]) { + сount[letter] -= 1; + } else { + сount[letter] = -1; + } + } + } + let remove = 0; + for (let count of Object.values(сount)) { + remove += Math.abs(count); + } + return remove; + } \ No newline at end of file diff --git a/codewars/Array Deep Count/deepcount.js b/codewars/Array Deep Count/deepcount.js new file mode 100644 index 000000000..495b7d4f9 --- /dev/null +++ b/codewars/Array Deep Count/deepcount.js @@ -0,0 +1,11 @@ +function deepCount(array) { + let sum = 0; + + for (let i of array) { + sum += 1; + if (Array.isArray(i)) { + sum += deepCount(i); + } + } + return sum; + } \ No newline at end of file diff --git a/codewars/Build Tower/buildtower.js b/codewars/Build Tower/buildtower.js new file mode 100644 index 000000000..f3e23c277 --- /dev/null +++ b/codewars/Build Tower/buildtower.js @@ -0,0 +1,12 @@ +function towerBuilder(numOfFloors) { + const tower = []; + for (let floor = 0; floor < numOfFloors; floor++) { + const numOfStars = 2 * floor + 1; + const numOfSpaces = numOfFloors - floor - 1; + const stars = '*'.repeat(numOfStars); + const spaces = ' '.repeat(numOfSpaces); + const floorString = spaces + stars + spaces; + tower.push(floorString); + } + return tower; + } \ No newline at end of file diff --git a/codewars/Convert string to camel case/camelcase.js b/codewars/Convert string to camel case/camelcase.js new file mode 100644 index 000000000..6daf0d54e --- /dev/null +++ b/codewars/Convert string to camel case/camelcase.js @@ -0,0 +1,15 @@ +function toCamelCase(str) { + if (str.length === 0) { + return str; + } + const arr = str.split(/[-_]/); + + const newArr = arr.map((word, index) => { + if (index === 0) { + return word[0] + word.substr(1).toLowerCase(); + } else { + return word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase(); + } + }); + return newArr.join(""); + } \ No newline at end of file diff --git a/codewars/Duplicate Encoder/dublicate.js b/codewars/Duplicate Encoder/dublicate.js new file mode 100644 index 000000000..e74b6dfbe --- /dev/null +++ b/codewars/Duplicate Encoder/dublicate.js @@ -0,0 +1,13 @@ +function duplicateEncode(word) { + word = word.toLowerCase(); + const count = {}; + for (const char of word) { + count[char] = (count[char] ?? 0) + 1; + } + + let brackets = ""; + for (const char of word) { + brackets += count[char] === 1 ? "(" : ")"; + } + return brackets; + } \ No newline at end of file diff --git a/codewars/Find the missing letter/missingletter.js b/codewars/Find the missing letter/missingletter.js new file mode 100644 index 000000000..933bd7c0f --- /dev/null +++ b/codewars/Find the missing letter/missingletter.js @@ -0,0 +1,9 @@ +function findMissingLetter(array) { + for (let i = 0; i < array.length - 1; i++) { + const icode = array[i].charCodeAt(0); + const inextcode = array[i + 1].charCodeAt(0); + if (inextcode !== icode + 1) { + return String.fromCharCode(icode + 1); + } + } + } \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/map.js b/codewars/Flatten a Nested Map/map.js new file mode 100644 index 000000000..04a125532 --- /dev/null +++ b/codewars/Flatten a Nested Map/map.js @@ -0,0 +1,24 @@ +function flattenMap(input) { + const result = {}; + + function flatten(current, prefix) { + for (const [key, value] of Object.entries(current)) { + const newKey = prefix ? `${prefix}/${key}` : key; + + if ( + value !== null && + typeof value === "object" && + !Array.isArray(value) && + typeof value !== "function" + ) { + flatten(value, newKey); + } else { + result[newKey] = value; + } + } + } + + flatten(input, ""); + + return result; + } \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/.gitkeep b/codewars/Fun with tree - max sum/.gitkeep index 8b1378917..e69de29bb 100644 --- a/codewars/Fun with tree - max sum/.gitkeep +++ b/codewars/Fun with tree - max sum/.gitkeep @@ -1 +0,0 @@ - diff --git a/codewars/Fun with tree - max sum/tree.js b/codewars/Fun with tree - max sum/tree.js new file mode 100644 index 000000000..6614c5991 --- /dev/null +++ b/codewars/Fun with tree - max sum/tree.js @@ -0,0 +1,18 @@ +function maxSum(root) { + if (root === null) { + return 0; + } + function dfs(node) { + if (node === null) { + return Number.NEGATIVE_INFINITY; + } + if (node.left === null && node.right === null) { + return node.value; + } + const leftSum = dfs(node.left); + const rightSum = dfs(node.right); + return node.value + Math.max(leftSum, rightSum); + } + + return dfs(root); + } \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/linkedlist.js b/codewars/Linked Lists - Sorted Insert/linkedlist.js new file mode 100644 index 000000000..433936ad5 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/linkedlist.js @@ -0,0 +1,19 @@ +function Node(data) { + this.data = data; + this.next = null; + } + + function sortedInsert(head, data) { + const newNode = new Node(data); + if (!head || data < head.data) { + newNode.next = head; + return newNode; + } + let current = head; + while (current.next && current.next.data < data) { + current = current.next; + } + newNode.next = current.next; + current.next = newNode; + return head; + } \ No newline at end of file diff --git a/codewars/Merge two arrays/merge2arrays.js b/codewars/Merge two arrays/merge2arrays.js new file mode 100644 index 000000000..003afb6d3 --- /dev/null +++ b/codewars/Merge two arrays/merge2arrays.js @@ -0,0 +1,12 @@ +function mergeArrays(a, b) { + let result = []; + let i = 0; + let j = 0; + + while (i < a.length || j < b.length) { + if (i < a.length) result.push(a[i++]); + if (j < b.length) result.push(b[j++]); + } + + return result; + } \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/movingzeros.js b/codewars/Moving Zeros To The End/movingzeros.js new file mode 100644 index 000000000..75c005cbf --- /dev/null +++ b/codewars/Moving Zeros To The End/movingzeros.js @@ -0,0 +1,14 @@ +function moveZeros(arr) { + const zeros = []; + const noZeros = []; + + for (let i = 0; i < arr.length; i++) { + if (arr[i] === 0) { + zeros.push(arr[i]); + } else { + noZeros.push(arr[i]); + } + } + zeros.forEach(zero => noZeros.push(zero)); + return noZeros; + } \ No newline at end of file diff --git a/codewars/Permutations/permutations.js b/codewars/Permutations/permutations.js new file mode 100644 index 000000000..2e657cde4 --- /dev/null +++ b/codewars/Permutations/permutations.js @@ -0,0 +1,18 @@ +function permutations(string) { + if (string.length <= 1) { + return [string]; + } + let shufflings = []; + for (let i = 0; i < string.length; i++) { + let char = string[i]; + if (string.indexOf(char) != i) { + continue; + } + let remainingStr = string.slice(0, i) + string.slice(i + 1); + for (let permutation of permutations(remainingStr)) { + shufflings.push(char + permutation); + } + } + + return [...new Set(shufflings)]; + } \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/productfib.js b/codewars/Product of consecutive Fib numbers/productfib.js new file mode 100644 index 000000000..c124c36f3 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/productfib.js @@ -0,0 +1,10 @@ +function productFib(prod) { + let first = 1; + let second = 1; + while (first * second < prod) { + let x = second; + second += first; + first = x; + } + return [first, second, first * second === prod]; + } \ No newline at end of file diff --git a/codewars/Simple Pig Latin/piglatin.js b/codewars/Simple Pig Latin/piglatin.js new file mode 100644 index 000000000..459f6ea5c --- /dev/null +++ b/codewars/Simple Pig Latin/piglatin.js @@ -0,0 +1,22 @@ +function pigIt(str) { + str = str.split(" "); + let ayLetters = "ay"; + let result = []; + + for (let word of str) { + if (word === "!" || word === "?") { + result.push(word); + break; + } + word = word.split(""); + let firstLetter = word.splice(0, 1)[0]; + word.push(firstLetter, ayLetters); + let pigIt = ""; + for (let char of word) { + pigIt += char; + } + result.push(pigIt); + } + let newStr = result.join(" "); + return newStr.trimEnd(); + } \ No newline at end of file diff --git a/codewars/Snail/snail.js b/codewars/Snail/snail.js new file mode 100644 index 000000000..d0b013aac --- /dev/null +++ b/codewars/Snail/snail.js @@ -0,0 +1,36 @@ +snail = function (array) { + if (array.length === 0) { + return []; + } + let bottom = 0; + let top = array.length - 1; + let left = 0; + let right = array[0].length - 1; + let arranged = []; + + while (bottom <= top && left <= right) { + for (let i = left; i <= right; i++) { + arranged.push(array[bottom][i]); + } + bottom++; + + for (let i = bottom; i <= top; i++) { + arranged.push(array[i][right]); + } + right--; + + if (bottom <= top && left <= right) { + for (let i = right; i >= left; i--) { + arranged.push(array[top][i]); + } + top--; + + for (let i = top; i >= bottom; i--) { + arranged.push(array[i][left]); + } + left++; + } + } + + return arranged; + }; \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/sumofdigits.js b/codewars/Sum of Digits - Digital Root/sumofdigits.js new file mode 100644 index 000000000..95ce76592 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/sumofdigits.js @@ -0,0 +1,14 @@ +function digitalRoot(n) { + let str = n.toString(); + let sum = 0; + + for (const char of str) { + let num = parseInt(char); + sum += num; + } + str = sum.toString(); + if (str.length > 1) { + return digitalRoot(str); + } + return sum; + } \ No newline at end of file diff --git a/codewars/Sum of Intervals/sumofintervals.js b/codewars/Sum of Intervals/sumofintervals.js new file mode 100644 index 000000000..237595341 --- /dev/null +++ b/codewars/Sum of Intervals/sumofintervals.js @@ -0,0 +1,22 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + + let sum = 0; + let End = Number; + + for (const [start, finish] of intervals) { + if (start <= End && finish >= End) { + sum += finish - End; + if (End < finish) { + End = finish; + } + } else if (End > finish) { + continue; + } else { + sum += finish - start; + End = finish; + } + } + + return sum; + } \ No newline at end of file diff --git a/codewars/Sum of pairs/sumofpairs,js b/codewars/Sum of pairs/sumofpairs,js new file mode 100644 index 000000000..8d84d4d91 --- /dev/null +++ b/codewars/Sum of pairs/sumofpairs,js @@ -0,0 +1,11 @@ +function sumPairs(ints, s) { + let noticed = {}; + for (let i = 0; i < ints.length; i++) { + let x = s - ints[i]; + if (x in noticed) { + return [x, ints[i]]; + } + noticed[ints[i]] = true; + } + return undefined; +} \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/tictac.js b/codewars/Tic-Tac-Toe Checker/tictac.js new file mode 100644 index 000000000..70f6e5c3c --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/tictac.js @@ -0,0 +1,42 @@ +function isSolved(board) { + for (let row = 0; row < 3; row++) { + if ( + board[row][0] !== 0 && + board[row][0] === board[row][1] && + board[row][0] === board[row][2] + ) { + return board[row][0]; + } + } + for (let col = 0; col < 3; col++) { + if ( + board[0][col] !== 0 && + board[0][col] === board[1][col] && + board[0][col] === board[2][col] + ) { + return board[0][col]; + } + } + if ( + board[0][0] !== 0 && + board[0][0] === board[1][1] && + board[0][0] === board[2][2] + ) { + return board[0][0]; + } + if ( + board[0][2] !== 0 && + board[0][2] === board[1][1] && + board[0][2] === board[2][0] + ) { + return board[0][2]; + } + for (let row = 0; row < 3; row++) { + for (let col = 0; col < 3; col++) { + if (board[row][col] === 0) { + return -1; + } + } + } + return 0; + } \ No newline at end of file diff --git a/codewars/Valid Parentheses/valid.js b/codewars/Valid Parentheses/valid.js new file mode 100644 index 000000000..329c5833a --- /dev/null +++ b/codewars/Valid Parentheses/valid.js @@ -0,0 +1,18 @@ +function validParentheses(parens) { + let stack = []; + + for (let i = 0; i < parens.length; i++) { + if (parens[i] === "(") { + stack.push("("); + } else if (parens[i] === ")") { + if (stack.length === 0) { + return false; + } else { + stack.pop(); + } + } + } + + return stack.length === 0; + } + \ No newline at end of file diff --git a/codewars/Where my anagrams at/anagrams.js b/codewars/Where my anagrams at/anagrams.js new file mode 100644 index 000000000..38a666213 --- /dev/null +++ b/codewars/Where my anagrams at/anagrams.js @@ -0,0 +1,5 @@ +function anagrams(word, words) { + const sortedWord = word.split("").sort().join(""); + return words.filter((w) => w.split("").sort().join("") === sortedWord); + } + \ No newline at end of file diff --git a/rpgsaga/.vscode/launch.json b/rpgsaga/.vscode/launch.json index b84fb7a7f..401656f68 100644 --- a/rpgsaga/.vscode/launch.json +++ b/rpgsaga/.vscode/launch.json @@ -12,7 +12,7 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}\\src\\index.ts", + "program": "${workspaceFolder}\\saga\\src\\index.ts", "outFiles": [ "${workspaceFolder}/**/*.js" ] @@ -21,7 +21,7 @@ "type": "node", "request": "launch", "name": "Tests debug", - "program": "${workspaceRoot}/node_modules/jest/bin/jest.js", + "program": "${workspaceRoot}/saga/node_modules/jest/bin/jest.js", "args": [ "-i" ], diff --git a/rpgsaga/saga/package-lock.json b/rpgsaga/saga/package-lock.json index 5d3f4f98c..ab210b9d0 100644 --- a/rpgsaga/saga/package-lock.json +++ b/rpgsaga/saga/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "ISC", "devDependencies": { - "@types/jest": "^27.0.3", + "@types/jest": "^27.5.2", "@types/node": "^16.11.0", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", @@ -1203,12 +1203,13 @@ } }, "node_modules/@types/jest": { - "version": "27.0.3", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.0.3.tgz", - "integrity": "sha512-cmmwv9t7gBYt7hNKH5Spu7Kuu/DotGa+Ff+JGRKZ4db5eh8PnKS4LuebJ3YLUoyOyIHraTGyULn23YtEAm0VSg==", + "version": "27.5.2", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz", + "integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==", "dev": true, + "license": "MIT", "dependencies": { - "jest-diff": "^27.0.0", + "jest-matcher-utils": "^27.0.0", "pretty-format": "^27.0.0" } }, @@ -7192,12 +7193,12 @@ } }, "@types/jest": { - "version": "27.0.3", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.0.3.tgz", - "integrity": "sha512-cmmwv9t7gBYt7hNKH5Spu7Kuu/DotGa+Ff+JGRKZ4db5eh8PnKS4LuebJ3YLUoyOyIHraTGyULn23YtEAm0VSg==", + "version": "27.5.2", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz", + "integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==", "dev": true, "requires": { - "jest-diff": "^27.0.0", + "jest-matcher-utils": "^27.0.0", "pretty-format": "^27.0.0" } }, diff --git a/rpgsaga/saga/package.json b/rpgsaga/saga/package.json index 1bb32af56..ab7b17e67 100644 --- a/rpgsaga/saga/package.json +++ b/rpgsaga/saga/package.json @@ -11,9 +11,8 @@ }, "author": "", "license": "ISC", - "dependencies": {}, "devDependencies": { - "@types/jest": "^27.0.3", + "@types/jest": "^27.5.2", "@types/node": "^16.11.0", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", @@ -23,9 +22,9 @@ "eslint-plugin-prettier": "^3.4.0", "jest": "^27.2.5", "prettier": "^2.4.1", + "ts-jest": "^27.1.2", "ts-node": "^10.3.0", - "typescript": "^4.4.4", - "ts-jest": "^27.1.2" + "typescript": "^4.4.4" }, "jest": { "preset": "ts-jest" From a6cdb2ce5d0cc1c5fdbe233867aaf8886be8cd29 Mon Sep 17 00:00:00 2001 From: kotleta96 Date: Thu, 24 Oct 2024 13:41:04 +0300 Subject: [PATCH 2/3] math function --- rpgsaga/saga/src/funcAndClass/function.ts | 50 +++++++++++++ rpgsaga/saga/src/index.ts | 3 +- rpgsaga/saga/tests/example.spec.ts | 86 +++++++++++++++++++++-- 3 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 rpgsaga/saga/src/funcAndClass/function.ts diff --git a/rpgsaga/saga/src/funcAndClass/function.ts b/rpgsaga/saga/src/funcAndClass/function.ts new file mode 100644 index 000000000..933f9889c --- /dev/null +++ b/rpgsaga/saga/src/funcAndClass/function.ts @@ -0,0 +1,50 @@ +//var 4 +function result(x: number): number { + const func = Math.pow((Math.abs(Math.pow(x, 2) - 2.5)), 1/4) + Math.pow((Math.log10(Math.pow(x, 2))), 1/3); + return func; +} + +function taskA(x_start: number, x_end: number, x_d: number): number[] { + const y = []; + if (x_d === 0) { + return []; + } else if (x_d > 0) { + for (let x = x_start; x <= x_end; x += x_d) { + y.push(result(x)); + } + } else { + for (let x = x_start; x >= x_end; x += x_d) { + y.push(result(x)); + } + } + return y; +} + +let x_start: number = 1.25; +let x_end: number = 3.25; +let x_d: number = 0.4; +console.log('Results of Task A for values:'); +console.log(x_start, x_end, x_d); +console.log(taskA(x_start, x_end, x_d)); + + + + +function taskB(values: number[]): number[] { + const y = []; + for (const x of values) { + y.push(result(x)); + } + return y; +} + +let x1: number = 1.84; +let x2: number = 2.71; +let x3: number = 3.81; +let x4: number = 4.56; +let x5: number = 5.62; +console.log("Results of Task B for values:"); +console.log(x1, x2, x3, x4, x5); +console.log(taskB([x1, x2, x3, x4, x5])); + +export {taskA, taskB}; diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7bc4a71de..3d8b4e288 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1 +1,2 @@ -console.log('Hello world'); + + \ No newline at end of file diff --git a/rpgsaga/saga/tests/example.spec.ts b/rpgsaga/saga/tests/example.spec.ts index e884fd1c4..32923bc20 100644 --- a/rpgsaga/saga/tests/example.spec.ts +++ b/rpgsaga/saga/tests/example.spec.ts @@ -1,7 +1,81 @@ -describe('Example', () => { - it('should return 5 as result of 2 and 3 sum', () => { - const a: number = 3; - const b: number = 2; - expect(a + b).toEqual(5); +import { taskA, taskB } from '../src/funcAndClass/function'; + +describe('Tests of taskA', () => { + it('1) default test', () => { + const testValues = [ + 1.5627118033190683, + 1.444483346894103, + 1.996584876888925, + 2.2878864247889314, + 2.508805958133056, + 2.692928296997281 + ]; + expect(taskA(1.25, 3.25, 0.4)).toEqual(testValues); }) -}); \ No newline at end of file + it('2) end < start, delta < 0', () => { + const testValues = [11.586775993365904, + 8.572690230403264, + Infinity, + 8.572690230403264, + 11.586775993365904]; + expect(taskA(100, -100, -50)).toEqual(testValues); + }) + it('3) end > start, delta > 0', () => { + expect(taskA(50, -50, 5)).toEqual([]); + }) + it('4) delta = 0', () => { + expect(taskA(2, 10, 0)).toEqual([]); + }) + it('5) delta < 0', () => { + expect(taskA(2, 10, -2)).toEqual([]); + }) + it('6) the function is not defined (return NaN)', () => { + expect(taskA(-0.81, 0.81, 0.81)).toEqual([NaN, Infinity, NaN]); + }) +}); + +describe('tests of taskB', () => { + it('7) default test', () => { + const testValues = [ + 1.7791658793122687, + 2.43670615212377, + 2.913109519389466, + 3.164503036090737, + 3.4668624552335574 + ] + expect(taskB([1.84, 2.71, 3.81, 4.56, 5.62])).toEqual(testValues);; + }) + it('8) must return empty array', () => { + expect(taskB([])).toEqual([]); + }) + it('9) just test', () => { + const testValues = [ + 1.726728684913989, + 2.431366913090221, + 2.9094657118289904, + 3.145765556689283, + 3.4615816198220877] + expect(taskB([1.8, 2.7, 3.8, 4.5, 5.6])).toEqual(testValues);; + }) + it('9) just test', () => { + const testValues = [ + 1.1066819197003217, + 1.9510787401896679, + 2.5812271977264367, + 2.980702641337647, + 3.296078572109559] + expect(taskB([1, 2, 3, 4, 5])).toEqual(testValues);; + }) + it('11) X-es < 0', () => { + const testValues = [ + 1.1066819197003217, + 1.9510787401896679, + 2.5812271977264367, + 2.980702641337647, + 3.296078572109559] + expect(taskB([-1, -2, -3, -4, -5])).toEqual(testValues);; + }) + it('12) the function is not defined (return NaN)', () => { + expect(taskB([-0.81, -0.4, 0, 0.4, 0.81])).toEqual([NaN, NaN, Infinity, NaN, NaN]); + }) +}) \ No newline at end of file From 8e6b093217a4db09c211a27900f62d98889d60c7 Mon Sep 17 00:00:00 2001 From: kotleta96 Date: Tue, 3 Dec 2024 00:26:48 +0300 Subject: [PATCH 3/3] class + tests --- rpgsaga/saga/src/funcAndClass/Car.ts | 66 ++++++++++++++++++++++++++++ rpgsaga/saga/tests/car.spec.ts | 54 +++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 rpgsaga/saga/src/funcAndClass/Car.ts create mode 100644 rpgsaga/saga/tests/car.spec.ts diff --git a/rpgsaga/saga/src/funcAndClass/Car.ts b/rpgsaga/saga/src/funcAndClass/Car.ts new file mode 100644 index 000000000..cf7d37196 --- /dev/null +++ b/rpgsaga/saga/src/funcAndClass/Car.ts @@ -0,0 +1,66 @@ +enum TransmissionEnum { + manual = 'manual', + automatic = 'automatic', + semiAutomatic = 'semi-automatic' +} + +class Car { + private _model: string; + private _speed: number; + private _transmission: string; + + constructor(model: string, speed: number, transmission: string) { + console.log('Constructor for Car is called'); + this._model = model; + this._speed = speed; + this._transmission = transmission; + } + + get model() { + return this._model; + } + + set model(newModel: string) { + this._model = newModel; + } + + get speed() { + return this._speed; + } + + set speed(newSpeed: number) { + if (this.speed < 0 && this.speed > 509) { + this._speed = newSpeed; + return;} + throw new Error('impossible speed'); + } + + get transmission() { + return this._transmission; + } + + set transmission(newTransmission: string) { + newTransmission = newTransmission.toLowerCase(); + if (newTransmission !== TransmissionEnum.manual && newTransmission !== TransmissionEnum.automatic && newTransmission !== TransmissionEnum.semiAutomatic) { + throw new Error('transmission only can be manual, automatic or semi-automatic'); + } + this._transmission = newTransmission; + return; + } + + transmissionType() { + if (this._transmission === TransmissionEnum.manual) {console.log('transmission type is manual')}; + if (this._transmission === TransmissionEnum.automatic) {console.log('transmission type is automatic')}; + if (this._transmission === TransmissionEnum.semiAutomatic) {console.log('transmission type is semi-automatic')};} + + + toString() { + return `model: ${this._model}, type: ${this._transmission}`; + } + + print() { + return `model: ${this._model}, type: ${this._transmission}, maxSpeed: ${this._speed}`; + } +} + +export {Car}; \ No newline at end of file diff --git a/rpgsaga/saga/tests/car.spec.ts b/rpgsaga/saga/tests/car.spec.ts new file mode 100644 index 000000000..dab2ff100 --- /dev/null +++ b/rpgsaga/saga/tests/car.spec.ts @@ -0,0 +1,54 @@ +import { Car } from "../src/funcAndClass/Car"; + +describe('test for constructor', () => { + it ('1) constructor', () => { + let newCar = new Car ('Volkswagen', 200, 'manual'); + expect(newCar.model).toBe('Volkswagen'); + expect(newCar.speed).toEqual(200); + expect(newCar.transmission).toBe('manual'); + })}) + +describe('tests for getters', () => { + let newCar = new Car ('Volkswagen', 200, 'manual'); + it ('2) get model', () => { + expect(newCar.model).toBe('Volkswagen');}) + it ('3) get speed', () => { + expect(newCar.speed).toEqual(200);}) + it ('4) get transmission', () => { + expect(newCar.transmission).toBe('manual');}) + }) + +describe('tests for setters', () => { + let newCar = new Car ('Volkswagen', 200, 'manual'); + it ('5) set model', () => { + newCar.model = 'Zhiguly'; + expect(newCar.model).toBe('Zhiguly');}) + + it ('6) set speed', () => { + expect(() => { + newCar.speed = 80; + expect(newCar.speed).toEqual(80);})}) + + it ('7) set impossible speed', () => { + expect(() => {newCar.speed = -200}).toThrow(Error('impossible speed'));}) + + it ('8) set transmission', () => { + newCar.transmission = 'semi-automatic'; + expect(newCar.transmission).toBe('semi-automatic');}) + + it ('9) set incorrect transmission', () => { + expect(() => {newCar.transmission = 'qwerty';}).toThrow(Error('transmission only can be manual, automatic or semi-automatic'));}) + }) + + +describe('tests for other methods', () => { + it('model and speed', () => { + let newCar = new Car('Volkswagen', 200, 'manual') + expect(newCar.toString()).toEqual(`model: ${newCar.model}, type: ${newCar.transmission}`); + }); + + it('display newCar', () => { + let newCar = new Car ('Volkswagen', 200, 'manual') + expect(newCar.print()).toEqual(`model: ${newCar.model}, type: ${newCar.transmission}, maxSpeed: ${newCar.speed}`); + }); + })