diff --git a/codewars/Anagram difference/main.js b/codewars/Anagram difference/main.js new file mode 100644 index 000000000..68ab3eb4a --- /dev/null +++ b/codewars/Anagram difference/main.js @@ -0,0 +1,25 @@ +function anagramDifference(w1, w2) { + const letterCounts1 = {}; + const letterCounts2 = {}; + + for (const letter of w1) { + letterCounts1[letter] = (letterCounts1[letter] || 0) + 1; + } + + for (const letter of w2) { + letterCounts2[letter] = (letterCounts2[letter] || 0) + 1; + } + + let difference = 0; + for (const letter in letterCounts1) { + difference += Math.abs(letterCounts1[letter] - (letterCounts2[letter] || 0)); + } + + for (const letter in letterCounts2) { + if (!letterCounts1[letter]) { + difference += letterCounts2[letter]; + } + } + + return difference; +} diff --git a/codewars/Array Deep Count/main.js b/codewars/Array Deep Count/main.js new file mode 100644 index 000000000..e5ad438d0 --- /dev/null +++ b/codewars/Array Deep Count/main.js @@ -0,0 +1,15 @@ +function deepCount(array) { + let count = 0; + + function countElements(arr) { + count += arr.length; + for (const item of arr) { + if (Array.isArray(item)) { + countElements(item); + } + } + } + + countElements(array); + return count; +} diff --git a/codewars/Build Tower/main.js b/codewars/Build Tower/main.js new file mode 100644 index 000000000..db48402c9 --- /dev/null +++ b/codewars/Build Tower/main.js @@ -0,0 +1,9 @@ +function towerBuilder(nFloors) { + const tower = []; + for (let i = 1; i <= nFloors; i++) { + const spaces = " ".repeat(nFloors - i); + const stars = "*".repeat(i * 2 - 1); + tower.push(spaces + stars + spaces); + } + return tower; +} diff --git a/codewars/Convert string to camel case/main.js b/codewars/Convert string to camel case/main.js new file mode 100644 index 000000000..3d72fec69 --- /dev/null +++ b/codewars/Convert string to camel case/main.js @@ -0,0 +1,11 @@ +function toCamelCase(str) { + const parts = str.replace(/_/g, '-').split('-'); + let result = [parts[0]]; + + for (let i = 1; i < parts.length; i++) { + result.push(parts[i][0].toUpperCase()); + result.push(parts[i].slice(1)); + } + + return result.join(''); +} \ No newline at end of file diff --git a/codewars/Duplicate Encoder/main.js b/codewars/Duplicate Encoder/main.js new file mode 100644 index 000000000..f955ae7ad --- /dev/null +++ b/codewars/Duplicate Encoder/main.js @@ -0,0 +1,16 @@ +function duplicateEncode(word) { + const lowerWord = word.toLowerCase(); + let result = ''; + + for (let i = 0; i < lowerWord.length; i++) { + const char = lowerWord[i]; + + if (lowerWord.split(char).length === 2) { + result += '('; + } else { + result += ')'; + } + } + + return result; +} \ No newline at end of file diff --git a/codewars/Find the missing letter/main.js b/codewars/Find the missing letter/main.js new file mode 100644 index 000000000..68bd0b92f --- /dev/null +++ b/codewars/Find the missing letter/main.js @@ -0,0 +1,9 @@ +function findMissingLetter(array) { + const string = array.join(''); + + for (let i = 0; i < string.length; i++) { + if (string.charCodeAt(i + 1) - string.charCodeAt(i) !== 1) { + return String.fromCharCode(string.charCodeAt(i) + 1); + } + } +} diff --git a/codewars/Flatten a Nested Map/main.js b/codewars/Flatten a Nested Map/main.js new file mode 100644 index 000000000..b65a03335 --- /dev/null +++ b/codewars/Flatten a Nested Map/main.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/main.js b/codewars/Fun with tree - max sum/main.js new file mode 100644 index 000000000..ce2dc8e65 --- /dev/null +++ b/codewars/Fun with tree - max sum/main.js @@ -0,0 +1,14 @@ +function maxSum(root) { + if (!root) return 0; + + if (!root.left && !root.right) return root.value; + + let leftMax = -Infinity; + let rightMax = -Infinity; + + if (root.left) leftMax = maxSum(root.left); + if (root.right) rightMax = maxSum(root.right); + + return root.value + Math.max(leftMax, rightMax); +} + diff --git a/codewars/Linked Lists - Sorted Insert/main.js b/codewars/Linked Lists - Sorted Insert/main.js new file mode 100644 index 000000000..eb7d56f00 --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/main.js @@ -0,0 +1,25 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +function sortedInsert(head, data) { + const newNode = new Node(data); + + if (head === null || data <= head.data) { + newNode.next = head; + return newNode; + } + + let current = head; + while (current.next !== null && data > current.next.data) { + current = current.next; + } + + newNode.next = current.next; + current.next = newNode; + + return head; +} diff --git a/codewars/Merge two arrays/main.js b/codewars/Merge two arrays/main.js new file mode 100644 index 000000000..6cbb88554 --- /dev/null +++ b/codewars/Merge two arrays/main.js @@ -0,0 +1,25 @@ +function mergeArrays(arr1, arr2) { + const result = []; + const maxLength = Math.max(arr1.length, arr2.length); + + + for (let i = 0; i < maxLength; i++) { + + if (i < arr1.length) { + result.push(arr1[i]); + } + + + if (i < arr2.length) { + result.push(arr2[i]); + } + } + + return result; +} + + +console.log(mergeArrays(['j', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5])); + + +console.log(mergeArrays([1, 2, 3], ['a', 'b', 'c', 'd', 'e', 'f'])); \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/main.js b/codewars/Moving Zeros To The End/main.js new file mode 100644 index 000000000..1c26babce --- /dev/null +++ b/codewars/Moving Zeros To The End/main.js @@ -0,0 +1,13 @@ +function moveZeros(arr) { + let nonZeroIndex = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== 0) { + arr[nonZeroIndex] = arr[i]; + nonZeroIndex++; + } + } + for (let i = nonZeroIndex; i < arr.length; i++) { + arr[i] = 0; + } + return arr; +} diff --git a/codewars/Product of consecutive Fib numbers/main.js b/codewars/Product of consecutive Fib numbers/main.js new file mode 100644 index 000000000..c509a9d64 --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/main.js @@ -0,0 +1,17 @@ +function productFib(prod) { + let f0 = 0; + let f1 = 1; + let found = false; + + while (f0 * f1 <= prod) { + if (f0 * f1 === prod) { + found = true; + break; + } + let temp = f1; + f1 = f0 + f1; + f0 = temp; + } + + return [f0, f1, found]; +} diff --git a/codewars/Simple Pig Latin/main.js b/codewars/Simple Pig Latin/main.js new file mode 100644 index 000000000..863aefc70 --- /dev/null +++ b/codewars/Simple Pig Latin/main.js @@ -0,0 +1,11 @@ +function pigIt(str){ + text = str.split(" "); + + for (var i = 0; i < text.length; i++) { + if (/[a-zA-Z]/.test(text[i])) { + text[i] = text[i].slice(1) + text[i][0] + 'ay'; + + } + } + return text.join(' ') +} \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/main.js b/codewars/Sum of Digits - Digital Root/main.js new file mode 100644 index 000000000..c037a3073 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/main.js @@ -0,0 +1,19 @@ +function digitalRoot(n) { + let str = n.toString(); + + let sum = 0; + for (let i = 0; i < str.length; i++) { + sum += parseInt(str[i], 10); + } + + while (sum > 9) { + let temp = 0; + while (sum > 0) { + temp += sum % 10; + sum = Math.floor(sum / 10); + } + sum = temp; + } + + return sum; +} diff --git a/codewars/Sum of pairs/main.js b/codewars/Sum of pairs/main.js new file mode 100644 index 000000000..45e38e54f --- /dev/null +++ b/codewars/Sum of pairs/main.js @@ -0,0 +1,12 @@ +function sumPairs(ints, s) { + const seen = new Map(); + for (let i = 0; i < ints.length; i++) { + const num = ints[i]; + const complement = s - num; + if (seen.has(complement)) { + return [complement, num]; + } + seen.set(num, i); + } + return undefined; +} diff --git a/codewars/Tic-Tac-Toe Checker/main.js b/codewars/Tic-Tac-Toe Checker/main.js new file mode 100644 index 000000000..d695eca91 --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/main.js @@ -0,0 +1,35 @@ +function isSolved(board) { + for (let i = 0; i < 3; i++) { + if (board[i][0] !== 0 && board[i][0] === board[i][1] && board[i][0] === board[i][2]) { + return board[i][0]; + } + } + + for (let i = 0; i < 3; i++) { + if (board[0][i] !== 0 && board[0][i] === board[1][i] && board[0][i] === board[2][i]) { + return board[0][i]; + } + } + + 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]; + } + + let draw = true; + for (let i = 0; i < 3; i++) { + for (let j = 0; j < 3; j++) { + if (board[i][j] === 0) { + draw = false; + break; + } + } + } + if (draw) { + return 0; + } + + return -1; +}