From e884b5a544335b0311fd02fea69edd981ecdd649 Mon Sep 17 00:00:00 2001 From: pvzprrrr Date: Mon, 30 Sep 2024 20:56:47 +0300 Subject: [PATCH 1/2] codewars --- codewars/Adding Big Numbers/BigNumber.js | 34 ++++++++++++++ codewars/Anagram difference/AnagramDiff.js | 24 ++++++++++ codewars/Array Deep Count/ArrayDeepCount.js | 14 ++++++ codewars/Build Tower/BuildTower.js | 11 +++++ .../ConvertStringToCamel.js | 11 +++++ codewars/Duplicate Encoder/Duplicate.js | 20 +++++++++ .../FindMissingLetter.js | 12 +++++ codewars/Flatten a Nested Map/NestedMap.js | 18 ++++++++ .../Fun with tree - max sum/TreeMaxSum.js | 16 +++++++ .../LinkedLists.js | 22 ++++++++++ codewars/Merge two arrays/MergeTwoArrays.js | 21 +++++++++ codewars/Moving Zeros To The End/ZeroToEnd.js | 15 +++++++ codewars/Permutations/Permutations.js | 17 +++++++ .../FibNumber.js | 14 ++++++ codewars/Simple Pig Latin/PigLatin.js | 15 +++++++ codewars/Snail/Snail.js | 31 +++++++++++++ .../SumOfDigits.js | 8 ++++ codewars/Sum of Intervals/SumOfIntervals.js | 18 ++++++++ codewars/Sum of pairs/SumOfPairs.js | 13 ++++++ .../Tic-Tac-Toe Checker/TicTaeToeChecker.js | 44 +++++++++++++++++++ rpgsaga/package-lock.json | 6 +++ 21 files changed, 384 insertions(+) create mode 100644 codewars/Adding Big Numbers/BigNumber.js create mode 100644 codewars/Anagram difference/AnagramDiff.js create mode 100644 codewars/Array Deep Count/ArrayDeepCount.js create mode 100644 codewars/Build Tower/BuildTower.js create mode 100644 codewars/Convert string to camel case/ConvertStringToCamel.js create mode 100644 codewars/Duplicate Encoder/Duplicate.js create mode 100644 codewars/Find the missing letter/FindMissingLetter.js create mode 100644 codewars/Flatten a Nested Map/NestedMap.js create mode 100644 codewars/Fun with tree - max sum/TreeMaxSum.js create mode 100644 codewars/Linked Lists - Sorted Insert/LinkedLists.js create mode 100644 codewars/Merge two arrays/MergeTwoArrays.js create mode 100644 codewars/Moving Zeros To The End/ZeroToEnd.js create mode 100644 codewars/Permutations/Permutations.js create mode 100644 codewars/Product of consecutive Fib numbers/FibNumber.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/TicTaeToeChecker.js create mode 100644 rpgsaga/package-lock.json diff --git a/codewars/Adding Big Numbers/BigNumber.js b/codewars/Adding Big Numbers/BigNumber.js new file mode 100644 index 00000000..a6587e5d --- /dev/null +++ b/codewars/Adding Big Numbers/BigNumber.js @@ -0,0 +1,34 @@ +function addBigNumber(a, b) { + const numLen = Math.max(a.length, b.length); + a = a.padStart(numLen, "0"); + b = b.padStart(numLen, "0"); + + let res = []; + + let remainder = false; + for (let i = a.length - 1; i >= 0; i--) { + let sum = Number(a[i]) + Number(b[i]); + + if (remainder) { + sum++; + remainder = false; + } + + sum = sum.toString(); + + if (sum.length > 1) { + remainder = true; + sum = sum.slice(1); + } + + res.push(sum); + } + + if (remainder) { + res.push("1"); + } + + res.reverse(); + res = res.join(""); + return res; +} \ No newline at end of file diff --git a/codewars/Anagram difference/AnagramDiff.js b/codewars/Anagram difference/AnagramDiff.js new file mode 100644 index 00000000..c1d60bed --- /dev/null +++ b/codewars/Anagram difference/AnagramDiff.js @@ -0,0 +1,24 @@ +function anagramDifference(w1, w2) { + let freq1 = {}; + let freq2 = {}; + let res = 0; + for (let char of w1) { + freq1[char] = (freq1[char] || 0) + 1; + } + for (let char of w2) { + freq2[char] = (freq2[char] || 0) + 1; + } + for (let char in freq1) { + if (freq2[char]) { + res += Math.abs(freq1[char] - freq2[char]); + } else { + res += freq1[char]; + } + } + for (let char in freq2) { + if (!freq1[char]) { + res += freq2[char]; + } + } + return res; +} \ No newline at end of file diff --git a/codewars/Array Deep Count/ArrayDeepCount.js b/codewars/Array Deep Count/ArrayDeepCount.js new file mode 100644 index 00000000..a39887d3 --- /dev/null +++ b/codewars/Array Deep Count/ArrayDeepCount.js @@ -0,0 +1,14 @@ +function deepCount(a) { + //... + if (a.length === 0) { + return 0; + } + let maxLength = 0; + for (let i = 0; i < a.length; i++) { + if (Array.isArray(a[i])) { + maxLength += deepCount(a[i]); + } + maxLength++; + } + return maxLength; +} \ 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 00000000..9d36977a --- /dev/null +++ b/codewars/Build Tower/BuildTower.js @@ -0,0 +1,11 @@ +function towerBuilder(nFloors) { + let stack = []; + let maxWidth = nFloors * 2 - 1; + for (let i = 0; i < nFloors; i++) { + let stars = i * 2 + 1; + let spaces = (maxWidth - stars) / 2; + let floor = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces); + stack.push(floor); + } + return stack; +} \ No newline at end of file diff --git a/codewars/Convert string to camel case/ConvertStringToCamel.js b/codewars/Convert string to camel case/ConvertStringToCamel.js new file mode 100644 index 00000000..01721b8f --- /dev/null +++ b/codewars/Convert string to camel case/ConvertStringToCamel.js @@ -0,0 +1,11 @@ +function toCamelCase(str) { + str = str.split(""); + for (let i = 0; i < str.length; i++) { + if (str[i] === "-" || str[i] === "_") { + str.splice(i, 2, str[i + 1].toUpperCase()); + } else { + continue; + } + } + return str.join(""); + } \ No newline at end of file diff --git a/codewars/Duplicate Encoder/Duplicate.js b/codewars/Duplicate Encoder/Duplicate.js new file mode 100644 index 00000000..ff921401 --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate.js @@ -0,0 +1,20 @@ +function duplicateEncode(word) { + let map = new Map(); + let arr = word.toLowerCase().split(""); + for (let item of arr) { + if (map.has(item)) { + map.set(item, map.get(item) + 1); + } else { + map.set(item, 1); + } + } + let res = []; + for (let i = 0; i < arr.length; i++) { + if (map.get(arr[i]) > 1) { + res.push(")"); + } else { + res.push("("); + } + } + return res.join(""); +} \ No newline at end of file diff --git a/codewars/Find the missing letter/FindMissingLetter.js b/codewars/Find the missing letter/FindMissingLetter.js new file mode 100644 index 00000000..f6e7bdf9 --- /dev/null +++ b/codewars/Find the missing letter/FindMissingLetter.js @@ -0,0 +1,12 @@ +function findMissingLetter(array) { + let caseWord = array[0] === array[0].toLowerCase(); + array = array.join('').toLowerCase().split(''); + let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); + let startIndex = alphabet.indexOf(array[0]); + for (let i = 0; i < array.length; i++) { + if (array[i] !== alphabet[startIndex + i]) { + return caseWord ? alphabet[startIndex + i] : alphabet[startIndex + i].toUpperCase(); + } + } + return undefined; +} \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/NestedMap.js b/codewars/Flatten a Nested Map/NestedMap.js new file mode 100644 index 00000000..eee42216 --- /dev/null +++ b/codewars/Flatten a Nested Map/NestedMap.js @@ -0,0 +1,18 @@ +function flattenMap(map, parentKey = "") { + let flatMap = {}; + for (let key in map) { + if (map.hasOwnProperty(key)) { + let newKey = parentKey ? `${parentKey}/${key}` : key; + if ( + typeof map[key] === "object" && + map[key] !== null && + !Array.isArray(map[key]) + ) { + Object.assign(flatMap, flattenMap(map[key], newKey)); + } else { + flatMap[newKey] = map[key]; + } + } + } + return flatMap; +} \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/TreeMaxSum.js b/codewars/Fun with tree - max sum/TreeMaxSum.js new file mode 100644 index 00000000..86f6bbd4 --- /dev/null +++ b/codewars/Fun with tree - max sum/TreeMaxSum.js @@ -0,0 +1,16 @@ +function maxSum(root) { + const maxSumRootToLeaf = (node) => { + if (node === null) { + return 0; + } + if (node.left === null && node.right === null) { + return node.value; + } + const leftSum = + node.left !== null ? maxSumRootToLeaf(node.left) : -Infinity; + const rightSum = + node.right !== null ? maxSumRootToLeaf(node.right) : -Infinity; + return node.value + Math.max(leftSum, rightSum); + }; + return maxSumRootToLeaf(root); +} \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/LinkedLists.js b/codewars/Linked Lists - Sorted Insert/LinkedLists.js new file mode 100644 index 00000000..427af131 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/LinkedLists.js @@ -0,0 +1,22 @@ +function Node(data) { + this.data = data; + this.next = null; +} + +function sortedInsert(head, data) { + const newNode = new Node(data); + if (!head) { + return newNode; + } + if (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/MergeTwoArrays.js b/codewars/Merge two arrays/MergeTwoArrays.js new file mode 100644 index 00000000..91b201f7 --- /dev/null +++ b/codewars/Merge two arrays/MergeTwoArrays.js @@ -0,0 +1,21 @@ +function mergeArrays(a, b) { + // your code here + let left = 0; + let right = 0; + let res = []; + while (left < a.length && right < b.length) { + res.push(a[left]); + left++; + res.push(b[right]); + right++; + } + while (left < a.length) { + res.push(a[left]); + left++; + } + while (right < b.length) { + res.push(b[right]); + right++; + } + return res; +} \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/ZeroToEnd.js b/codewars/Moving Zeros To The End/ZeroToEnd.js new file mode 100644 index 00000000..d3a0e453 --- /dev/null +++ b/codewars/Moving Zeros To The End/ZeroToEnd.js @@ -0,0 +1,15 @@ +function moveZeros(arr) { + let counter = 0; + let newArr = []; + for (let item of arr) { + if (item === 0) { + counter += 1; + } else { + newArr.push(item); + } + } + for (let i = 0; i < counter; i++) { + newArr.push(0); + } + return newArr; +} \ No newline at end of file diff --git a/codewars/Permutations/Permutations.js b/codewars/Permutations/Permutations.js new file mode 100644 index 00000000..bdb2ac30 --- /dev/null +++ b/codewars/Permutations/Permutations.js @@ -0,0 +1,17 @@ +function permutations(string) { + let res = []; + let perm = (arr, currentArr) => { + if (arr.length === 0) { + res.push(currentArr.join("")); + return; + } + + for (let i = 0; i < arr.length; i++) { + let nextArr = arr.slice(0, i).concat(arr.slice(i + 1)); + perm(nextArr, currentArr.concat(arr[i])); + } + }; + + perm(string.split(""), []); + return [...new Set(res)]; +} \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/FibNumber.js b/codewars/Product of consecutive Fib numbers/FibNumber.js new file mode 100644 index 00000000..3b01e981 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/FibNumber.js @@ -0,0 +1,14 @@ +function productFib(prod) { + let a = 0; + let b = 1; + while (a * b < prod) { + let next = a + b; + a = b; + b = next; + } + if (a * b === prod) { + return [a, b, true]; + } else { + return [a, b, false]; + } +} \ 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 00000000..7b789144 --- /dev/null +++ b/codewars/Simple Pig Latin/PigLatin.js @@ -0,0 +1,15 @@ +function pigIt(str) { + //Code here + str = str.split(" "); + let arr = []; + for (let i = 0; i < str.length; i++) { + let newStr = ""; + if (str[i] === "!" || str[i] === "?") { + newStr = str[i].split("").slice(1).concat(str[i][0]); + } else { + newStr = str[i].split("").slice(1).concat(str[i][0], "ay"); + } + arr.push(newStr.join("")); + } + return arr.join(" "); +} \ No newline at end of file diff --git a/codewars/Snail/Snail.js b/codewars/Snail/Snail.js new file mode 100644 index 00000000..b20004b4 --- /dev/null +++ b/codewars/Snail/Snail.js @@ -0,0 +1,31 @@ +function snail(array) { + let res = []; + let left = 0; + let right = array[0].length - 1; + let top = 0; + let bottom = array.length - 1; + while (left <= right && top <= bottom) { + for (let i = left; i <= right; i++) { + res.push(array[top][i]); + } + top += 1; + for (let i = top; i <= bottom; i++) { + res.push(array[i][right]); + } + right -= 1; + if (top <= bottom) { + for (let i = right; i >= left; i--) { + res.push(array[bottom][i]); + } + bottom -= 1; + } + if (left <= right) { + for (let i = bottom; i >= top; i--) { + res.push(array[i][left]); + } + left += 1; + } + } + + return res; +} \ 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 00000000..441cda74 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/SumOfDigits.js @@ -0,0 +1,8 @@ +function digitalRoot(n) { + if (n < 10) { + return n; + } + return digitalRoot( + n.toString().split("").reduce((summ, digit) => summ + +digit, 0) + ); +} \ 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 00000000..c9853ca0 --- /dev/null +++ b/codewars/Sum of Intervals/SumOfIntervals.js @@ -0,0 +1,18 @@ +function sumIntervals(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + let stack = []; + let currentInterval = intervals[0]; + for (let i = 1; i < intervals.length; i++) { + if (intervals[i][0] <= currentInterval[1]) { + currentInterval[1] = Math.max(currentInterval[1], intervals[i][1]); + } else { + stack.push(currentInterval); + currentInterval = intervals[i]; + } + } + stack.push(currentInterval); + let lengthOfIntervals = stack + .map((item) => item[1] - item[0]) + .reduce((summ, digits) => summ + digits, 0); + return lengthOfIntervals; + } \ 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 00000000..370a5b6d --- /dev/null +++ b/codewars/Sum of pairs/SumOfPairs.js @@ -0,0 +1,13 @@ +function sumPairs(ints, s) { + let seen = new Set(); + + for (let i = 0; i < ints.length; i++) { + let current = ints[i]; + let needed = s - current; + if (seen.has(needed)) { + return [ints[ints.indexOf(needed)], ints[i]]; + } + seen.add(current); + } + return undefined; +} \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/TicTaeToeChecker.js b/codewars/Tic-Tac-Toe Checker/TicTaeToeChecker.js new file mode 100644 index 00000000..f79ee720 --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/TicTaeToeChecker.js @@ -0,0 +1,44 @@ +function isSolved(board) { + for (let i = 0; i < 3; i++) { + if ( + board[i][0] !== 0 && + board[i][0] === board[i][1] && + board[i][1] === board[i][2] + ) { + return board[i][0]; + } + if ( + board[0][i] !== 0 && + board[0][i] === board[1][i] && + board[1][i] === board[2][i] + ) { + return board[0][i]; + } + } + + if ( + board[0][0] !== 0 && + board[0][0] === board[1][1] && + board[1][1] === board[2][2] + ) { + return board[0][0]; + } + + if ( + board[0][2] !== 0 && + board[0][2] === board[1][1] && + board[1][1] === board[2][0] + ) { + return board[0][2]; + } + + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) { + return -1; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/rpgsaga/package-lock.json b/rpgsaga/package-lock.json new file mode 100644 index 00000000..f09b1837 --- /dev/null +++ b/rpgsaga/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "rpgsaga", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} From 0ad9115149bdfee6fd901b2b5beec560ba3a2327 Mon Sep 17 00:00:00 2001 From: pvzprrrr Date: Tue, 1 Oct 2024 16:55:37 +0300 Subject: [PATCH 2/2] codewars --- codewars/Adding Big Numbers/BigNumber.js | 54 +++++++++---------- codewars/Anagram difference/AnagramDiff.js | 38 ++++++------- codewars/Array Deep Count/ArrayDeepCount.js | 9 ++-- codewars/Build Tower/BuildTower.js | 12 ++--- .../ConvertStringToCamel.js | 18 +++---- codewars/Duplicate Encoder/Duplicate.js | 18 +++---- .../FindMissingLetter.js | 10 ++-- codewars/Flatten a Nested Map/NestedMap.js | 8 +-- .../Fun with tree - max sum/TreeMaxSum.js | 8 +-- codewars/Merge two arrays/MergeTwoArrays.js | 13 +++-- codewars/Moving Zeros To The End/ZeroToEnd.js | 14 ++--- codewars/Permutations/Permutations.js | 6 +-- codewars/Simple Pig Latin/PigLatin.js | 9 ++-- codewars/Snail/Snail.js | 28 +++++----- codewars/Sum of Intervals/SumOfIntervals.js | 32 +++++------ 15 files changed, 137 insertions(+), 140 deletions(-) diff --git a/codewars/Adding Big Numbers/BigNumber.js b/codewars/Adding Big Numbers/BigNumber.js index a6587e5d..d82446c5 100644 --- a/codewars/Adding Big Numbers/BigNumber.js +++ b/codewars/Adding Big Numbers/BigNumber.js @@ -2,33 +2,33 @@ function addBigNumber(a, b) { const numLen = Math.max(a.length, b.length); a = a.padStart(numLen, "0"); b = b.padStart(numLen, "0"); - - let res = []; - - let remainder = false; + + let listy = []; + + let tailing = false; for (let i = a.length - 1; i >= 0; i--) { - let sum = Number(a[i]) + Number(b[i]); - - if (remainder) { - sum++; - remainder = false; - } - - sum = sum.toString(); - - if (sum.length > 1) { - remainder = true; - sum = sum.slice(1); - } - - res.push(sum); + let sum = Number(a[i]) + Number(b[i]); + + if (tailing) { + sum++; + tailing = false; + } + + sum = sum.toString(); + + if (sum.length > 1) { + tailing = true; + sum = sum.slice(1); + } + + listy.push(sum); } - - if (remainder) { - res.push("1"); + + if (tailing) { + listy.push("1"); } - - res.reverse(); - res = res.join(""); - return res; -} \ No newline at end of file + + listy.reverse(); + listy = listy.join(""); + return listy; + } \ No newline at end of file diff --git a/codewars/Anagram difference/AnagramDiff.js b/codewars/Anagram difference/AnagramDiff.js index c1d60bed..41f53dd5 100644 --- a/codewars/Anagram difference/AnagramDiff.js +++ b/codewars/Anagram difference/AnagramDiff.js @@ -1,24 +1,24 @@ function anagramDifference(w1, w2) { - let freq1 = {}; - let freq2 = {}; - let res = 0; - for (let char of w1) { - freq1[char] = (freq1[char] || 0) + 1; + let rate1 = {}; + let rate2 = {}; + let listy = 0; + for (let sign of w1) { + rate1[sign] = (rate1[sign] || 0) + 1; } - for (let char of w2) { - freq2[char] = (freq2[char] || 0) + 1; + for (let sign of w2) { + rate2[sign] = (rate2[sign] || 0) + 1; } - for (let char in freq1) { - if (freq2[char]) { - res += Math.abs(freq1[char] - freq2[char]); - } else { - res += freq1[char]; - } + for (let sign in rate1) { + if (rate2[sign]) { + listy += Math.abs(rate1[sign] - rate2[sign]); + } else { + listy += rate1[sign]; + } } - for (let char in freq2) { - if (!freq1[char]) { - res += freq2[char]; - } + for (let sign in rate2) { + if (!rate1[sign]) { + listy += rate2[sign]; + } } - return res; -} \ No newline at end of file + return listy; + } \ No newline at end of file diff --git a/codewars/Array Deep Count/ArrayDeepCount.js b/codewars/Array Deep Count/ArrayDeepCount.js index a39887d3..3e9c5fba 100644 --- a/codewars/Array Deep Count/ArrayDeepCount.js +++ b/codewars/Array Deep Count/ArrayDeepCount.js @@ -1,14 +1,13 @@ function deepCount(a) { - //... if (a.length === 0) { return 0; } - let maxLength = 0; + let mxLen = 0; for (let i = 0; i < a.length; i++) { if (Array.isArray(a[i])) { - maxLength += deepCount(a[i]); + mxLen += deepCount(a[i]); } - maxLength++; + mxLen++; } - return maxLength; + return mxLen; } \ No newline at end of file diff --git a/codewars/Build Tower/BuildTower.js b/codewars/Build Tower/BuildTower.js index 9d36977a..56a602eb 100644 --- a/codewars/Build Tower/BuildTower.js +++ b/codewars/Build Tower/BuildTower.js @@ -1,11 +1,11 @@ function towerBuilder(nFloors) { - let stack = []; - let maxWidth = nFloors * 2 - 1; + let listy = []; + let mxWid = nFloors * 2 - 1; for (let i = 0; i < nFloors; i++) { let stars = i * 2 + 1; - let spaces = (maxWidth - stars) / 2; - let floor = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces); - stack.push(floor); + let spaces = (mxWid - stars) / 2; + let level = " ".repeat(spaces) + "*".repeat(stars) + " ".repeat(spaces); + listy.push(level); } - return stack; + return listy; } \ No newline at end of file diff --git a/codewars/Convert string to camel case/ConvertStringToCamel.js b/codewars/Convert string to camel case/ConvertStringToCamel.js index 01721b8f..4fddf7aa 100644 --- a/codewars/Convert string to camel case/ConvertStringToCamel.js +++ b/codewars/Convert string to camel case/ConvertStringToCamel.js @@ -1,11 +1,11 @@ function toCamelCase(str) { - str = str.split(""); - for (let i = 0; i < str.length; i++) { - if (str[i] === "-" || str[i] === "_") { - str.splice(i, 2, str[i + 1].toUpperCase()); - } else { - continue; - } + str = str.split(""); + for (let i = 0; i < str.length; i++) { + if (str[i] === "-" || str[i] === "_") { + str.splice(i, 2, str[i + 1].toUpperCase()); + } else { + continue; } - return str.join(""); - } \ No newline at end of file + } + return str.join(""); +} \ No newline at end of file diff --git a/codewars/Duplicate Encoder/Duplicate.js b/codewars/Duplicate Encoder/Duplicate.js index ff921401..592fdfe2 100644 --- a/codewars/Duplicate Encoder/Duplicate.js +++ b/codewars/Duplicate Encoder/Duplicate.js @@ -1,20 +1,20 @@ function duplicateEncode(word) { - let map = new Map(); + let scheme = new Map(); let arr = word.toLowerCase().split(""); for (let item of arr) { - if (map.has(item)) { - map.set(item, map.get(item) + 1); + if (scheme.has(item)) { + scheme.set(item, scheme.get(item) + 1); } else { - map.set(item, 1); + scheme.set(item, 1); } } - let res = []; + let listy = []; for (let i = 0; i < arr.length; i++) { - if (map.get(arr[i]) > 1) { - res.push(")"); + if (scheme.get(arr[i]) > 1) { + listy.push(")"); } else { - res.push("("); + listy.push("("); } } - return res.join(""); + return listy.join(""); } \ No newline at end of file diff --git a/codewars/Find the missing letter/FindMissingLetter.js b/codewars/Find the missing letter/FindMissingLetter.js index f6e7bdf9..5d6552d6 100644 --- a/codewars/Find the missing letter/FindMissingLetter.js +++ b/codewars/Find the missing letter/FindMissingLetter.js @@ -1,11 +1,11 @@ function findMissingLetter(array) { - let caseWord = array[0] === array[0].toLowerCase(); + let vocabulary = array[0] === array[0].toLowerCase(); array = array.join('').toLowerCase().split(''); - let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''); - let startIndex = alphabet.indexOf(array[0]); + let alph = 'abcdefghijklmnopqrstuvwxyz'.split(''); + let firsrIndex = alph.indexOf(array[0]); for (let i = 0; i < array.length; i++) { - if (array[i] !== alphabet[startIndex + i]) { - return caseWord ? alphabet[startIndex + i] : alphabet[startIndex + i].toUpperCase(); + if (array[i] !== alph[firsrIndex + i]) { + return vocabulary ? alph[firsrIndex + i] : alph[firsrIndex + i].toUpperCase(); } } return undefined; diff --git a/codewars/Flatten a Nested Map/NestedMap.js b/codewars/Flatten a Nested Map/NestedMap.js index eee42216..936ebd33 100644 --- a/codewars/Flatten a Nested Map/NestedMap.js +++ b/codewars/Flatten a Nested Map/NestedMap.js @@ -1,5 +1,5 @@ function flattenMap(map, parentKey = "") { - let flatMap = {}; + let MapFlat = {}; for (let key in map) { if (map.hasOwnProperty(key)) { let newKey = parentKey ? `${parentKey}/${key}` : key; @@ -8,11 +8,11 @@ function flattenMap(map, parentKey = "") { map[key] !== null && !Array.isArray(map[key]) ) { - Object.assign(flatMap, flattenMap(map[key], newKey)); + Object.assign(MapFlat, flattenMap(map[key], newKey)); } else { - flatMap[newKey] = map[key]; + MapFlat[newKey] = map[key]; } } } - return flatMap; + return MapFlat; } \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/TreeMaxSum.js b/codewars/Fun with tree - max sum/TreeMaxSum.js index 86f6bbd4..e87115d6 100644 --- a/codewars/Fun with tree - max sum/TreeMaxSum.js +++ b/codewars/Fun with tree - max sum/TreeMaxSum.js @@ -1,5 +1,5 @@ function maxSum(root) { - const maxSumRootToLeaf = (node) => { + const FromBeginningToEnd = (node) => { if (node === null) { return 0; } @@ -7,10 +7,10 @@ function maxSum(root) { return node.value; } const leftSum = - node.left !== null ? maxSumRootToLeaf(node.left) : -Infinity; + node.left !== null ? FromBeginningToEnd(node.left) : -Infinity; const rightSum = - node.right !== null ? maxSumRootToLeaf(node.right) : -Infinity; + node.right !== null ? FromBeginningToEnd(node.right) : -Infinity; return node.value + Math.max(leftSum, rightSum); }; - return maxSumRootToLeaf(root); + return FromBeginningToEnd(root); } \ No newline at end of file diff --git a/codewars/Merge two arrays/MergeTwoArrays.js b/codewars/Merge two arrays/MergeTwoArrays.js index 91b201f7..4044131b 100644 --- a/codewars/Merge two arrays/MergeTwoArrays.js +++ b/codewars/Merge two arrays/MergeTwoArrays.js @@ -1,21 +1,20 @@ function mergeArrays(a, b) { - // your code here let left = 0; let right = 0; - let res = []; + let listy = []; while (left < a.length && right < b.length) { - res.push(a[left]); + listy.push(a[left]); left++; - res.push(b[right]); + listy.push(b[right]); right++; } while (left < a.length) { - res.push(a[left]); + listy.push(a[left]); left++; } while (right < b.length) { - res.push(b[right]); + listy.push(b[right]); right++; } - return res; + return listy; } \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/ZeroToEnd.js b/codewars/Moving Zeros To The End/ZeroToEnd.js index d3a0e453..6504e97d 100644 --- a/codewars/Moving Zeros To The End/ZeroToEnd.js +++ b/codewars/Moving Zeros To The End/ZeroToEnd.js @@ -1,15 +1,15 @@ function moveZeros(arr) { - let counter = 0; - let newArr = []; + let count = 0; + let listy = []; for (let item of arr) { if (item === 0) { - counter += 1; + count += 1; } else { - newArr.push(item); + listy.push(item); } } - for (let i = 0; i < counter; i++) { - newArr.push(0); + for (let i = 0; i < count; i++) { + listy.push(0); } - return newArr; + return listy; } \ No newline at end of file diff --git a/codewars/Permutations/Permutations.js b/codewars/Permutations/Permutations.js index bdb2ac30..1fed8746 100644 --- a/codewars/Permutations/Permutations.js +++ b/codewars/Permutations/Permutations.js @@ -1,8 +1,8 @@ function permutations(string) { - let res = []; + let listy = []; let perm = (arr, currentArr) => { if (arr.length === 0) { - res.push(currentArr.join("")); + listy.push(currentArr.join("")); return; } @@ -13,5 +13,5 @@ function permutations(string) { }; perm(string.split(""), []); - return [...new Set(res)]; + return [...new Set(listy)]; } \ No newline at end of file diff --git a/codewars/Simple Pig Latin/PigLatin.js b/codewars/Simple Pig Latin/PigLatin.js index 7b789144..a99d12d5 100644 --- a/codewars/Simple Pig Latin/PigLatin.js +++ b/codewars/Simple Pig Latin/PigLatin.js @@ -1,15 +1,14 @@ function pigIt(str) { - //Code here str = str.split(" "); let arr = []; for (let i = 0; i < str.length; i++) { - let newStr = ""; + let str1 = ""; if (str[i] === "!" || str[i] === "?") { - newStr = str[i].split("").slice(1).concat(str[i][0]); + str1 = str[i].split("").slice(1).concat(str[i][0]); } else { - newStr = str[i].split("").slice(1).concat(str[i][0], "ay"); + str1 = str[i].split("").slice(1).concat(str[i][0], "ay"); } - arr.push(newStr.join("")); + arr.push(str1.join("")); } return arr.join(" "); } \ No newline at end of file diff --git a/codewars/Snail/Snail.js b/codewars/Snail/Snail.js index b20004b4..6cb08211 100644 --- a/codewars/Snail/Snail.js +++ b/codewars/Snail/Snail.js @@ -1,31 +1,31 @@ function snail(array) { - let res = []; + let listy = []; let left = 0; let right = array[0].length - 1; - let top = 0; - let bottom = array.length - 1; - while (left <= right && top <= bottom) { + let upper = 0; + let lower = array.length - 1; + while (left <= right && upper <= lower) { for (let i = left; i <= right; i++) { - res.push(array[top][i]); + listy.push(array[upper][i]); } - top += 1; - for (let i = top; i <= bottom; i++) { - res.push(array[i][right]); + upper += 1; + for (let i = upper; i <= lower; i++) { + listy.push(array[i][right]); } right -= 1; - if (top <= bottom) { + if (upper <= lower) { for (let i = right; i >= left; i--) { - res.push(array[bottom][i]); + listy.push(array[lower][i]); } - bottom -= 1; + lower -= 1; } if (left <= right) { - for (let i = bottom; i >= top; i--) { - res.push(array[i][left]); + for (let i = lower; i >= upper; i--) { + listy.push(array[i][left]); } left += 1; } } - return res; + return listy; } \ No newline at end of file diff --git a/codewars/Sum of Intervals/SumOfIntervals.js b/codewars/Sum of Intervals/SumOfIntervals.js index c9853ca0..f5ce88f9 100644 --- a/codewars/Sum of Intervals/SumOfIntervals.js +++ b/codewars/Sum of Intervals/SumOfIntervals.js @@ -1,18 +1,18 @@ function sumIntervals(intervals) { - intervals.sort((a, b) => a[0] - b[0]); - let stack = []; - let currentInterval = intervals[0]; - for (let i = 1; i < intervals.length; i++) { - if (intervals[i][0] <= currentInterval[1]) { - currentInterval[1] = Math.max(currentInterval[1], intervals[i][1]); - } else { - stack.push(currentInterval); - currentInterval = intervals[i]; - } + intervals.sort((a, b) => a[0] - b[0]); + let listy = []; + let actualInt = intervals[0]; + for (let i = 1; i < intervals.length; i++) { + if (intervals[i][0] <= actualInt[1]) { + actualInt[1] = Math.max(actualInt[1], intervals[i][1]); + } else { + listy.push(actualInt); + actualInt = intervals[i]; } - stack.push(currentInterval); - let lengthOfIntervals = stack - .map((item) => item[1] - item[0]) - .reduce((summ, digits) => summ + digits, 0); - return lengthOfIntervals; - } \ No newline at end of file + } + listy.push(actualInt); + let lengthOfIntervals = listy + .map((item) => item[1] - item[0]) + .reduce((summ, digits) => summ + digits, 0); + return lengthOfIntervals; +} \ No newline at end of file