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
20 changes: 20 additions & 0 deletions codewars/Adding Big Numbers/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function add(a, b) {
let result = '';
let carry = 0;
let maxLength = Math.max(a.length, b.length);

a = a.padStart(maxLength, '0');
b = b.padStart(maxLength, '0');

for (let i = maxLength - 1; i >= 0; i--) {
let sum = parseInt(a[i]) + parseInt(b[i]) + carry;
carry = Math.floor(sum / 10);
result = (sum % 10) + result;
}

if (carry) {
result = carry + result;
}

return result;
}
25 changes: 25 additions & 0 deletions codewars/Anagram difference/anagramDifference.js
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions codewars/Array Deep Count/deepCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function deepCount(array) {
let k = 0;
const countElements = arr => {
k += arr.length;
for (let i of arr) {
if (Array.isArray(i) ) {
countElements(i);
}
}
}
countElements(array);
return k;
}
9 changes: 9 additions & 0 deletions codewars/Build Tower/towerBuilder.js
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions codewars/Convert string to camel case/toCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function toCamelCase(str) {
a = str.replaceAll('_', '-').split('-');
res = []
res.push(a[0]);
for(i = 1;i < a.length;i++){
res.push(a[i][0].toUpperCase())
res.push(a[i].slice(1))
}
return res.join("")
}
12 changes: 12 additions & 0 deletions codewars/Duplicate Encoder/duplicateEncode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function duplicateEncode(word) {
word = word.toLowerCase();
let result = '';
for (let i = 0; i < word.length; i++) {
if (word.lastIndexOf(word[i]) === word.indexOf(word[i])) {
result += '(';
} else {
result += ')';
}
}
return result;
}
8 changes: 8 additions & 0 deletions codewars/Find the missing letter/findMissingLetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function findMissingLetter(array) {
var string = array.join("");
for (var i = 0; i < string.length; i++) {
if (string.charCodeAt(i + 1) - string.charCodeAt(i) !== 1) {
return String.fromCharCode(string.charCodeAt(i) + 1);
}
}
}
23 changes: 23 additions & 0 deletions codewars/Flatten a Nested Map/flattenMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;
}
13 changes: 13 additions & 0 deletions codewars/Fun with tree - max sum/maxSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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);
}
23 changes: 23 additions & 0 deletions codewars/Linked Lists - Sorted Insert/sortedInsert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function Node(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;
}
17 changes: 17 additions & 0 deletions codewars/Merge two arrays/mergeArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function mergeArrays(a, b) {
let mergedArray = [];
let i = 0;
let j = 0;

while (i < a.length || j < b.length) {
if (i < a.length) {
mergedArray.push(a[i]);
i += 1;
}
if (j < b.length) {
mergedArray.push(b[j]);
j += 1;
}
}
return mergedArray;
}
13 changes: 13 additions & 0 deletions codewars/Moving Zeros To The End/moveZeros.js
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions codewars/Permutations/permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function permutations(string) {
if (string.length === 1) {
return [string];
}

const result = [];
for (let i = 0; i < string.length; i++) {
const char = string[i];
const remainingChars = string.slice(0, i) + string.slice(i + 1);
const subPermutations = permutations(remainingChars);

for (const subPermutation of subPermutations) {
result.push(char + subPermutation);
}
}

return [...new Set(result)];
}
17 changes: 17 additions & 0 deletions codewars/Product of consecutive Fib numbers/productFib.js
Original file line number Diff line number Diff line change
@@ -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];
}
12 changes: 12 additions & 0 deletions codewars/Simple Pig Latin/pigIt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function pigIt(str) {
return str
.split(" ")
.map((word) => {
if (word.match(/[a-zA-Z]/)) {
return word.slice(1) + word.charAt(0) + "ay";
} else {
return word;
}
})
.join(" ");
}
37 changes: 37 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function snail(array) {
if (array.length === 0) return [];

const result = [];
let top = 0;
let bottom = array.length - 1;
let left = 0;
let right = array[0].length - 1;

while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) {
result.push(array[top][i]);
}
top++;

for (let i = top; i <= bottom; i++) {
result.push(array[i][right]);
}
right--;

if (top <= bottom && left <= right) {
for (let i = right; i >= left; i--) {
result.push(array[bottom][i]);
}
bottom--;
}

if (top <= bottom && left <= right) {
for (let i = bottom; i >= top; i--) {
result.push(array[i][left]);
}
left++;
}
}

return result;
}
19 changes: 19 additions & 0 deletions codewars/Sum of Digits - Digital Root/digitalRoot.js
Original file line number Diff line number Diff line change
@@ -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.charAt(i));
}

while (sum > 9) {
let temp = 0;
while (sum > 0) {
temp += sum % 10;
sum = Math.floor(sum / 10);
}
sum = temp;
}

return sum;
}
21 changes: 21 additions & 0 deletions codewars/Sum of Intervals/sumIntervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function sumIntervals(intervals) {
intervals.sort((a, b) => a[0] - b[0]);
let mergedIntervals = [intervals[0]];

for (let i = 1; i < intervals.length; i++) {
let lastInterval = mergedIntervals[mergedIntervals.length - 1];

if (intervals[i][0] <= lastInterval[1]) {
lastInterval[1] = Math.max(lastInterval[1], intervals[i][1]);
} else {
mergedIntervals.push(intervals[i]);
}
}

let totalLength = 0;
for (let interval of mergedIntervals) {
totalLength += interval[1] - interval[0];
}

return totalLength;
}
12 changes: 12 additions & 0 deletions codewars/Sum of pairs/sumPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function sumPairs(ints, s) {
let seen = new Map();
for (let i = 0; i < ints.length; i++) {
let num = ints[i];
let complement = s - num;
if (seen.has(complement)) {
return [complement, num];
}
seen.set(num, i);
}
return undefined;
}
35 changes: 35 additions & 0 deletions codewars/Tic-Tac-Toe Checker/isSolved.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading