-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function anagramDifference(w1, w2) { | ||
if (w1 === "" && w2 === "") { | ||
return 0; | ||
} else if (w1 === "" || w2 === "") { | ||
if (w1 !== "") { | ||
return w1.length; | ||
} else { | ||
return w2.length; | ||
} | ||
} | ||
let letterCount = {}; | ||
if (w1 !== "" && w2 !== "") { | ||
for (let letter of w1) { | ||
letterCount[letter] = (letterCount[letter] || 0) + 1; | ||
} | ||
|
||
for (let letter of w2) { | ||
if (letterCount[letter]) { | ||
letterCount[letter]--; | ||
} else { | ||
letterCount[letter] = -1; | ||
} | ||
} | ||
} | ||
let lettersToRemove = 0; | ||
|
||
for (let count of Object.values(letterCount)) { | ||
lettersToRemove += Math.abs(count); | ||
} | ||
|
||
return lettersToRemove; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function deepCount(a) { | ||
let sumTotal = 0; | ||
|
||
for (let element of a) { | ||
sumTotal += 1; | ||
if (Array.isArray(element)) { | ||
sumTotal += deepCount(element); | ||
} | ||
} | ||
|
||
return sumTotal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
16 changes: 16 additions & 0 deletions
16
codewars/Convert string to camel case/convert_string_to_camel_case.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function toCamelCase(str) { | ||
if (str == "") { | ||
return str; | ||
} | ||
const words = str.split(/[-_]/); | ||
|
||
const upperCaseWords = words.map((word, index) => { | ||
if (index === 0) { | ||
return word.charAt(0) + word.slice(1).toLowerCase(); | ||
} else { | ||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | ||
} | ||
}); | ||
|
||
return upperCaseWords.join(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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; | ||
} |
10 changes: 10 additions & 0 deletions
10
codewars/Find the missing letter/find_the_missing_letter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function findMissingLetter(array) { | ||
for (let i = 0; i < array.length - 1; i++) { | ||
const currChar = array[i].charCodeAt(0); | ||
const nextChar = array[i + 1].charCodeAt(0); | ||
|
||
if (nextChar !== currChar + 1) { | ||
return String.fromCharCode(currChar + 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function flattenMap(input) { | ||
// Initialize the result object | ||
const result = {}; | ||
|
||
// Recursive helper function | ||
function flatten(current, prefix) { | ||
for (const [key, value] of Object.entries(current)) { | ||
const newKey = prefix ? `${prefix}/${key}` : key; // Create the new key with prefix | ||
|
||
if ( | ||
value !== null && | ||
typeof value === "object" && | ||
!Array.isArray(value) && | ||
typeof value !== "function" | ||
) { | ||
// If the value is an object (but not null, array, or function), recurse | ||
flatten(value, newKey); | ||
} else { | ||
// Otherwise, assign the value directly to the result | ||
result[newKey] = value; | ||
} | ||
} | ||
} | ||
|
||
// Start the flattening process with the initial input | ||
flatten(input, ""); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function mergeArrays(a, b) { | ||
let resultLength = Math.max(a.length, b.length) * 2; | ||
let merge = [a, b]; | ||
let result = []; | ||
|
||
for (let i = 0; i < resultLength; i++) { | ||
let value = merge[i % 2][Math.floor(i / 2)]; | ||
if (value != undefined) result.push(value); | ||
} | ||
|
||
return result; | ||
} |
18 changes: 18 additions & 0 deletions
18
codewars/Moving Zeros To The End/moving_Zeros_To_The_End.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function moveZeros(arr) { | ||
const zero = []; | ||
const arr2 = []; | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === 0) { | ||
zero.push(arr[i]); | ||
} else { | ||
arr2.push(arr[i]); | ||
} | ||
} | ||
|
||
for (let j = 0; j < zero.length; j++) { | ||
arr2.push(zero[j]); | ||
} | ||
|
||
return arr2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function permutations(string) { | ||
if (string.length <= 1) { | ||
return [string]; | ||
} | ||
let result = []; | ||
for (let i = 0; i < string.length; i++) { | ||
let char = string[i]; | ||
if (string.indexOf(char) != i) { | ||
continue; | ||
} | ||
let remainingStr = string.slice(0, i) + string.slice(i + 1); | ||
for (let permutation of permutations(remainingStr)) { | ||
result.push(char + permutation); | ||
} | ||
} | ||
|
||
return [...new Set(result)]; | ||
} |
9 changes: 9 additions & 0 deletions
9
codewars/Product of consecutive Fib numbers/product_of_consecutive_Fib_numbers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function digitalRoot(n) { | ||
let str = n.toString(); | ||
let result = 0; | ||
|
||
for (const char of str) { | ||
let num = parseInt(char); | ||
result += num; | ||
} | ||
|
||
str = result.toString(); | ||
if (str.length >= "2") { | ||
return digitalRoot(str); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function sumIntervals(intervals) { | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
let sum = 0; | ||
let currentEnd = -Infinity; | ||
|
||
for (const [start, end] of intervals) { | ||
if (start <= currentEnd && end >= currentEnd) { | ||
sum += end - currentEnd; | ||
currentEnd = Math.max(currentEnd, end); | ||
} else if (end < currentEnd) { | ||
continue; | ||
} else { | ||
sum += end - start; | ||
currentEnd = end; | ||
} | ||
} | ||
|
||
return sum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 j = 0; j < 3; j++) { | ||
if ( | ||
board[0][j] !== 0 && | ||
board[0][j] === board[1][j] && | ||
board[0][j] === board[2][j] | ||
) { | ||
return board[0][j]; | ||
} | ||
} | ||
|
||
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]; | ||
} | ||
|
||
for (let i = 0; i < 3; i++) { | ||
for (let j = 0; j < 3; j++) { | ||
if (board[i][j] === 0) { | ||
return -1; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |