Skip to content
19 changes: 19 additions & 0 deletions codewars/Adding Big Numbers/Adding Big Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function add(num1, num2) {
let result = '';
let carry = 0;
let i = num1.length - 1;
let j = num2.length - 1;
while (i >= 0 || j >= 0 || carry > 0) {
const digit1 = i >= 0 ? parseInt(num1[i]) : 0;
const digit2 = j >= 0 ? parseInt(num2[j]) : 0;

const sum = digit1 + digit2 + carry;
result = (sum % 10) + result;
carry = Math.floor(sum / 10);

i--;
j--;
}

return result;
}
28 changes: 28 additions & 0 deletions codewars/Anagram difference/Anagram defferende.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function anagramDifference(w1, w2) {
const letters1 = {};
const letters2 = {};

for (let i = 0; i < w1.length; i++) {
const letter = w1[i];
letters1[letter] = (letters1[letter] || 0) + 1;
}

for (let i = 0; i < w2.length; i++) {
const letter = w2[i];
letters2[letter] = (letters2[letter] || 0) + 1;
}

let difference = 0;
for (const letter in letters1) {
difference += Math.abs((letters1[letter] || 0) - (letters2[letter] || 0));
}
for (const letter in letters2) {
if (!(letter in letters1)) {
difference += letters2[letter];
}
}

return difference;
}


16 changes: 16 additions & 0 deletions codewars/Array Deep Count/Array Deep Count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;

function deepCount(arr) {
let count = 0;

for (const element of arr) {
count++;
if (Array.isArray(element)) {
count += deepCount(element);
}
}

return count;
}
19 changes: 19 additions & 0 deletions codewars/Build Tower/Build Tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function towerBuilder(nFloors) {
const tower = [];
for (let i = 0; i < nFloors; i++) {
const stars = (i * 2) + 1; // Количество звездочек на каждой строке
const spaces = nFloors - i - 1; // Количество пробелов по краям
let row = "";
for (let j = 0; j < spaces; j++) {
row += " ";
}
for (let j = 0; j < stars; j++) {
row += "*";
}
for (let j = 0; j < spaces; j++) {
row += " ";
}
tower.push(row);
}
return tower;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function toCamelCase(str) {
if (!str) {
return "";
}

const words = str.split(/[-_]/);
const camelCaseWords = words.map((word, index) => {
if (index === 0) {
return word;
} else {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
}
});

return camelCaseWords.join("");
}
16 changes: 16 additions & 0 deletions codewars/Duplicate Encoder/Duplicate Encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function duplicateEncode(word) {
const lowerWord = word.toLowerCase();
const charCount = {};
let result = '';

for (const char of lowerWord) {
charCount[char] = (charCount[char] || 0) + 1;
}

for (const char of lowerWord) {
result += charCount[char] === 1 ? '(' : ')';


}

}
27 changes: 27 additions & 0 deletions codewars/Find the missing letter/Find the missing letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function findMissingLetter(array) {
2
let startLetter = array[0].charCodeAt(0);
3
let missingLetter = "";
4
5
for (let i = 1; i < array.length; i++) {
6
let currentLetter = array[i].charCodeAt(0);
7
if (currentLetter - startLetter !== i) {
8
missingLetter = String.fromCharCode(startLetter + i);
9
break;
10
}
11
}
12
13
return missingLetter;
14
}
14 changes: 14 additions & 0 deletions codewars/Flatten a Nested Map/Flatten a Nested Map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function flattenMap(obj, parentKey = '', result = {}) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}/${key}` : key;
const value = obj[key];
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
flattenMap(value, newKey, result);
} else {
result[newKey] = value;
}
}
}
return result;
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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;
let previous = null;

while (current !== null && data >= current.data) {
previous = current;
current = current.next;
}

newNode.next = previous.next;
previous.next = newNode;

return head;
}
18 changes: 18 additions & 0 deletions codewars/Merge two arrays/Merge two arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function mergeArrays(chars, nums) {
let merged = [];
let i = 0;
let j = 0;

while (i < chars.length || j < nums.length) {
if (i < chars.length) {
merged.push(chars[i]);
i++;
}
if (j < nums.length) {
merged.push(nums[j]);
j++;
}
}

return merged;
}
18 changes: 18 additions & 0 deletions codewars/Moving Zeros To The End/Moving Zeros To The End.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function moveZeros(arr) {
const result = [];
let zeroCount = 0;

for (const element of arr) {
if (element === 0) {
zeroCount++;
} else {
result.push(element);
}
}

for (let i = 0; i < zeroCount; i++) {
result.push(0);
}

return result;
}
19 changes: 19 additions & 0 deletions codewars/Permutations/Permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function permutations(string) {
const result = new Set();

function backtrack(path, remaining) {
if (remaining.length === 0) {
result.add(path);
return;
}
for (let i = 0; i < remaining.length; i++) {
const nextPath = path + remaining[i];
const nextRemaining = remaining.slice(0, i) + remaining.slice(i + 1);
backtrack(nextPath, nextRemaining);
}
}

backtrack('', string);
return Array.from(result);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function productFib(prod) {
let a = 0;
let b = 1;

while (a * b < prod) {
[a, b] = [b, a + b];
}

return [a, b, a * b === prod];
}
12 changes: 12 additions & 0 deletions codewars/Simple Pig Latin/Simple Pig Latin.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[0] + 'ay';
} else {
return word;
}
})
.join(' ');
}
43 changes: 43 additions & 0 deletions codewars/Snail/Snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
snail = function(array) {
if (array.length === 0 || array[0].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) {

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

if (left <= right) {

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

return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function digitalRoot(n) {
if (n < 10) {
return n;
}

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

return digitalRoot(sum);
}
Empty file.
34 changes: 34 additions & 0 deletions codewars/Tic-Tac-Toe Checker/Tic-Tac-Toe Cheacker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function isSolved(board) {
function checkWinner(player) {
for (let i = 0; i < 3; i++) {
if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
return true;
}
}
for (let i = 0; i < 3; i++) {
if (board[0][i] === player && board[1][i] === player && board[2][i] === player) {
return true;
}
}
if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
return true;
}
if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
return true;
}
return false;
}

if (checkWinner(1)) return 1; // Игрок 1 победил
if (checkWinner(2)) return 2; // Игрок 2 победил

for (let row of board) {
for (let cell of row) {
if (cell === 0) {
return -1; // Игра продолжается
}
}
}

return 0; // Ничья
}
17 changes: 17 additions & 0 deletions codewars/Valid Parentheses/Valid Parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function validParentheses(s) {
let balance = 0;

for (let char of s) {
if (char === '(') {
balance++;
} else if (char === ')') {
balance--;
}

if (balance < 0) {
return false;
}
}

return balance === 0;
}
Loading
Loading