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
13 changes: 13 additions & 0 deletions codewars/Adding Big Numbers/addingbignumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
37 changes: 37 additions & 0 deletions codewars/Anagram difference/anagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function anagramDifference(w1,w2){
const frequencyMap1 = {};
const frequencyMap2 = {};

for (const letter of w1) {
if (letter in frequencyMap1) {
frequencyMap1[letter]++;
} else {
frequencyMap1[letter] = 1;
}
}

for (const letter of w2) {
if (letter in frequencyMap2) {
frequencyMap2[letter]++;
} else {
frequencyMap2[letter] = 1;
}
}

let totalLettersToRemove = 0;
for (const letter in frequencyMap1) {
if (letter in frequencyMap2) {
totalLettersToRemove += Math.abs(frequencyMap1[letter] - frequencyMap2[letter]);
} else {
totalLettersToRemove += frequencyMap1[letter];
}
}

for (const letter in frequencyMap2) {
if (!(letter in frequencyMap1)) {
totalLettersToRemove += frequencyMap2[letter];
}
}

return totalLettersToRemove;
}
11 changes: 11 additions & 0 deletions codewars/Array Deep Count/arraydeepcount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function deepCount(a){
let count = 0;

for (let i of a) {
count += 1;
if (Array.isArray(i)){
count += deepCount(i);
}
}
return count;
}
10 changes: 10 additions & 0 deletions codewars/Build Tower/buildtower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function towerBuilder(nFloors) {
const tower = [];

for (let i = 0; i < nFloors; i++) {
const stars = "*".repeat(2 * i + 1);
const spaces = " ".repeat(nFloors - i - 1);
tower.push(spaces + stars + spaces);
}
return tower;
}
14 changes: 14 additions & 0 deletions codewars/Convert string to camel case/convetring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function toCamelCase(str) {
if (str == "") {
return str;
}
const words = str.split(/[-_]/);

const upperCaseWords = words.map((word, index) => {
return index === 0
? word.charAt(0) + word.slice(1).toLowerCase()
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
});

return upperCaseWords.join("");
}
14 changes: 14 additions & 0 deletions codewars/Duplicate Encoder/duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function duplicateEncode(word){
word = word.toLowerCase();

const charCount = {};

for (const char of word) {
charCount[char] = (charCount[char] || 0) + 1;
}
let result = "";
for (const char of word) {
result += charCount[char] === 1 ? "(" : ")";
}
return result;
}
12 changes: 12 additions & 0 deletions codewars/Find the missing letter/missingletter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function findMissingLetter(array)
{
for (let i = 0; i < array.length - 1; i++) {

const firstLetter = array[i].charCodeAt(0);
const secondLetter = array[i + 1].charCodeAt(0);

if (firstLetter !== secondLetter - 1) {
return String.fromCharCode(secondLetter - 1);
};
};
};
23 changes: 23 additions & 0 deletions codewars/Flatten a Nested Map/nestedmap.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;
}
32 changes: 32 additions & 0 deletions codewars/Fun with tree - max sum/maxsum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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);
}

console.log(
maxSum(
new TreeNode(
5,
new TreeNode(4, new TreeNode(-80), new TreeNode(-60)),
new TreeNode(10, new TreeNode(-90))
)
)
);
19 changes: 19 additions & 0 deletions codewars/Linked Lists - Sorted Insert/sortedinsert.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions codewars/Merge two arrays/mergetwoarrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function mergeArrays(a, b) {
let lengthRes = Math.max(a.length, b.length) * 2;
let merge = [a, b];
let result = [];

for (let i = 0; i < lengthRes; i++) {
let value = merge[i % 2][Math.floor(i / 2)];
if (value != undefined) result.push(value);
}
return result;
}
console.log(mergeArrays(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5]));
17 changes: 17 additions & 0 deletions codewars/Moving Zeros To The End/movingzeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function moveZeros(arr) {
let zeros = [];
let others = [];

for (let i = 0; i < arr.length; i++) {
if (arr[i] === 0) {
zeros.push(arr[i]);
} else {
others.push(arr[i]);
}
}
for (let j = 0; j < zeros.length; j++) {
others.push(zeros[j]);
}

return others
}
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];
}
let results = [];
for (let i = 0; i < string.length; i++) {
let firstChar = string[i];
if (string.indexOf(firstChar) != i) {
continue;
}
let otherChars = string.slice(0, i) + string.slice(i + 1);
for (let permutation of permutations(otherChars)) {
results.push(firstChar + permutation);
}
}
return [...new Set(results)];
}
9 changes: 9 additions & 0 deletions codewars/Product of consecutive Fib numbers/fibnumb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function productFib(prod){
let first = 1;
let second = 1;
while (first * second < prod) {
second += first;
first = second - first;
}
return [first, second, first * second === prod];
}
29 changes: 29 additions & 0 deletions codewars/Simple Pig Latin/piglatin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function pigIt(str) {
str = str.split(" ");
let lastLetters = "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, lastLetters);
let resultWord = "";

for (let char of word) {
resultWord += char;
}
result.push(resultWord);
}

let resStr = "";
result.forEach((str) => {
resStr += `${str} ` ;
});

return resStr.trimEnd();
}
37 changes: 37 additions & 0 deletions codewars/Snail/snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
snail = function (array) {
if (array.length === 0) {
return [];
}

let 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--;

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

return result;
};
13 changes: 13 additions & 0 deletions codewars/Sum of Digits - Digital Root/sumofdigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function digitalRoot(n) {
let num = n.toString();
let sum = 0;

for (i = 0; i < num.length; i++) {
sum += parseInt(num[i]);
}

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

for(const [start, end] of intervals){
if (start <= currEnd && end >= currEnd){
sum += end - currEnd
currEnd = Math.max(currEnd, end);
} else if (end < currEnd){
continue;
} else {
sum += end - start;
currEnd = end;
}
}
return sum;
}
11 changes: 11 additions & 0 deletions codewars/Sum of pairs/sumofpairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function sumPairs(ints, s) {
let saw = {};
for (let i = 0; i < ints.length; i++){
let comp = s - ints[i];
if(comp in saw){
return[comp, ints[i]];
}
saw [ints[i]] = i;
}
return undefined;
}
Loading
Loading