Skip to content

Commit

Permalink
codewsars
Browse files Browse the repository at this point in the history
  • Loading branch information
ZinixZay committed Jan 6, 2024
1 parent a36ee9a commit 2fcdf98
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 0 deletions.
19 changes: 19 additions & 0 deletions codewars/Anagram difference/anagramDifference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function anagramDifference(w1,w2){
var count = 0;

for (var i = 0; i < w1.length; i++){
if (w2.indexOf(w1[i]) >= 0){

w2 = w2.replace(w1[i], '')

} else {

count++

}
}
return w2.length + count
}


console.log(anagramDifference('codewars', 'hackerrank'))
19 changes: 19 additions & 0 deletions codewars/Array Deep Count/arrayDeepCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function deepCount(a){
var counter = 0

function recursive(a){
for (var i = 0; i < a.length; i++){
if (Array.isArray(a[i])){
counter++
recursive(a[i])
} else {
counter++
}
}
}

recursive(a)
return counter
}

console.log(deepCount(["x", "y", ["z"]]))
13 changes: 13 additions & 0 deletions codewars/Build Tower/buildTower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function towerBuilder(nFloor){
var towerArray = []
for (var i = 0; i < nFloor; i++){
var star = "*".repeat(1 + i * 2)
var space = " ".repeat(nFloor - i - 1)
var floor = space + star + space

towerArray.push(floor)
}
return towerArray
}

console.log()
12 changes: 12 additions & 0 deletions codewars/Convert string to camel case/convertStringToCamesCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function toCamelCase(str){

var newStrArr = str.split(/[_-]/);
for (var i = 1; i < newStrArr.length; i++){
newStrArr[i] = newStrArr[i][0].toUpperCase() + newStrArr[i].substring(1);
}
return newStrArr.join('');
}

console.log(toCamelCase("the-stealth-warrior"))
console.log(toCamelCase("The_Stealth_Warrior"))
console.log(toCamelCase("The_Stealth-Warrior"))
25 changes: 25 additions & 0 deletions codewars/Duplicate Encoder/duplicateEncoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function duplicateEncode(word){

word = word.toLowerCase();
var newstr = "";
var count = 0;
for (let i = 0; i < word.length; i++){
count = 0
for (let j = 0; j < word.length; j++){
if (word[j] === word[i]){
count ++
}
}
if (count > 1) {
newstr += ")"
} else {
newstr += "("
}
}
return newstr
}

console.log(duplicateEncode('din'))
console.log(duplicateEncode('recede'))
console.log(duplicateEncode('Success'))
console.log(duplicateEncode('(( @'))
14 changes: 14 additions & 0 deletions codewars/Find the missing letter/findTheMissingLetter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
array = ["O", "Q", "R", "S"]

console.log(findMissingLetter(array))

function findMissingLetter(charArray){
var idx = 0
firstLetter = charArray[0].charCodeAt(idx)
for (var i = 1; i < charArray.length; i++){
currentLetter = charArray[i].charCodeAt(idx)
if (1 < currentLetter - firstLetter){
return String.fromCharCode(firstLetter + 1)
}
}
}
26 changes: 26 additions & 0 deletions codewars/Merge two arrays/mergeTwoArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var a = ['a', 'b', 'c', 'd', 'e']

var b = [1, 2, 3, 4, 5]

function mergeArray(a, b) {

var res = []
var biggestLengthOfArray = 0
if (a.length > b.length){
biggestLengthOfArray = a.length
} else if (a.length == b.length){
biggestLengthOfArray = b.length
} else {
biggestLengthOfArray = b.length
}

for (var i = 0; i < biggestLengthOfArray; i++) {
if (a[i] !== undefined) res.push(a[i])
if (b[i] !== undefined) res.push(b[i])
}

return res
}


console.log(mergeArray(a, b))
22 changes: 22 additions & 0 deletions codewars/Moving Zeros To The End/movingZerosToTheEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const arr = [false,1,0,1,2,0,1,3,"a"]

function moveZeros(arr) {
var emptyArray = []
var counter = 0

for (var i = 0; i < arr.length; i++){

if (arr[i] !== 0){
emptyArray.push(arr[i])
} else {
counter++
}
}
for (var j = 0; j < counter; j++){
emptyArray.push(0)
}

return emptyArray
}

console.log(moveZeros(arr))
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function productFib(prod) {

var FibFirst = 0
var FibSecond = 1

while (FibFirst * FibSecond < prod){
const store = FibSecond
FibSecond = FibSecond + FibFirst
FibFirst = store

}
return [FibFirst, FibSecond, FibFirst * FibSecond === prod];
}


console.log(productFib(714))
console.log(productFib(800))
18 changes: 18 additions & 0 deletions codewars/Simple Pig Latin/simplePigLatin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function pigIt(str) {

var words = str.split(' ');

function convertWord(word) {
if (/^[A-Za-z]+$/.test(word)) {
return word.slice(1) + word[0] + 'ay';
} else {
return word;
}
}

var pigLatinWords = words.map(convertWord);

var pigLatinStr = pigLatinWords.join(' ');

return pigLatinStr;
}
27 changes: 27 additions & 0 deletions codewars/Sum of Digits - Digital Root/sumOfDigitsDigitalRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function digitalRoot(number){

var numberStr = String(number)
numberStr = numberStr.split("")

if (number > 9){
var sum = 0
for (var i = 0; i < numberStr.length; i++ ){

sum += Number(numberStr[i])

}

return digitalRoot(sum)

} else {

return number

}
}

console.log(digitalRoot(16))
console.log(digitalRoot(942))
console.log(digitalRoot(132189))
console.log(digitalRoot(493193))
console.log(digitalRoot(10))
17 changes: 17 additions & 0 deletions codewars/Sum of pairs/sumOfPairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function sumPairs(ints, s) {
var second_index = +Infinity
var answer = undefined;
for (let i = 0; i < ints.length - 1; i++){
for (let j = i + 1; j < ints.length; j++){
if ((ints[i] + ints[j]) == s){
if (second_index > j){
second_index = j
answer = [ints[i], ints[j]]
}
}
}
}
return answer
}

console.log(sumPairs([10, 5, 2, 3, 7, 5], 10))
14 changes: 14 additions & 0 deletions codewars/Valid Parentheses/validParenthleses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function validParentheses(parens) {
var count = 0
for (i = 0; i < parens.length; i++){
if (parens[i] == '('){
count++
} else {
count--
}
}
if (count == 0){
return true
}
return false
}
21 changes: 21 additions & 0 deletions codewars/Where my anagrams at/whereMyAnagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function anagrams(word, words) {
let answer = []
for (let words_key in words){
let candidate = words[words_key]
let anagram = true
for (let word_key in word){
if (words[words_key].indexOf(word[word_key]) >= 0){
words[words_key] = words[words_key].replace(word[word_key], '')
} else {
anagram = false
break
}
}
if (words[words_key].length == 0 && anagram){
answer.push(candidate)
}
}
return answer
}

console.log(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']))

0 comments on commit 2fcdf98

Please sign in to comment.