diff --git a/easy/count-instances-of-a-character-in-a-string/code.js b/easy/count-instances-of-a-character-in-a-string/code.js index b5b604c4..a4fad1c5 100644 --- a/easy/count-instances-of-a-character-in-a-string/code.js +++ b/easy/count-instances-of-a-character-in-a-string/code.js @@ -1,5 +1,11 @@ function charCount(myChar, str) { - // Your code here. + let counted = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === myChar) { + counted++; + } + } + return counted; } module.exports = charCount; diff --git a/easy/count-ones-in-a-2d-array/code.js b/easy/count-ones-in-a-2d-array/code.js index df564211..b3980f43 100644 --- a/easy/count-ones-in-a-2d-array/code.js +++ b/easy/count-ones-in-a-2d-array/code.js @@ -1,5 +1,13 @@ function countOnes(matrix) { - // Your code here. + let counted = 0; + for (let i = 0; i < matrix.length; i++) { + for (let k = 0; k < matrix[i].length; k++) { + if (matrix[i][k] === 1) { + counted++ + } + } + } + return counted; } module.exports = countOnes; diff --git a/easy/even-or-odd-number-of-factors/code.js b/easy/even-or-odd-number-of-factors/code.js index fe1c107d..d1c633bf 100644 --- a/easy/even-or-odd-number-of-factors/code.js +++ b/easy/even-or-odd-number-of-factors/code.js @@ -1,5 +1,9 @@ function factorGroup(num) { - // Your code here. + if (num % 2 === 0) { + return "odd"; + } else { + return "even"; + } } module.exports = factorGroup; diff --git a/easy/factor-chain/code.js b/easy/factor-chain/code.js index a6b05e04..7985c589 100644 --- a/easy/factor-chain/code.js +++ b/easy/factor-chain/code.js @@ -1,5 +1,10 @@ function factorChain(arr) { - // Your code here. + for (let i = 0; i < arr.length - 1; i++) { + if (arr[i + 1] % arr[i] !== 0) { + return false; + } + } + return true; } module.exports = factorChain; diff --git a/easy/filter-out-strings-from-an-array/code.js b/easy/filter-out-strings-from-an-array/code.js index c2e4d25f..49ade9c5 100644 --- a/easy/filter-out-strings-from-an-array/code.js +++ b/easy/filter-out-strings-from-an-array/code.js @@ -1,5 +1,11 @@ function filterArray(arr) { - // Your code here. + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if (typeof(arr[i]) === "number") { + newArr.push(arr[i]) + } + } + return newArr; } module.exports = filterArray; diff --git a/easy/find-the-largest-numbers-in-a-group-of-arrays/code.js b/easy/find-the-largest-numbers-in-a-group-of-arrays/code.js index b52b989c..8692849a 100644 --- a/easy/find-the-largest-numbers-in-a-group-of-arrays/code.js +++ b/easy/find-the-largest-numbers-in-a-group-of-arrays/code.js @@ -1,5 +1,15 @@ function findLargestNums(arr) { - // Your code here. + const newArr = []; + for (let i = 0; i < arr.length; i++) { + let biggest = arr[i][0] + for (let k = 0; k < arr[i].length; k++) { + if (arr[i][k] > biggest) { + biggest = arr[i][k] + } + } + newArr.push(biggest); + } + return newArr; } module.exports = findLargestNums; diff --git a/easy/find-the-minimum-maximum-length-and-average-values/code.js b/easy/find-the-minimum-maximum-length-and-average-values/code.js index 6a77a369..ba54bb4f 100644 --- a/easy/find-the-minimum-maximum-length-and-average-values/code.js +++ b/easy/find-the-minimum-maximum-length-and-average-values/code.js @@ -1,5 +1,23 @@ function minMaxLengthAverage(arr) { - // Your code here. + const newArr = []; + let min = arr[0]; + let max = arr[0]; + let total = 0; + for (let i = 0; i < arr.length; i++) { + if (min > arr[i]) { + min = arr[i] + } + if (max < arr[i]) { + max = arr[i] + } + total += arr[i] + } + newArr.push(min); + newArr.push(max); + newArr.push(arr.length); + newArr.push(total / arr.length); + + return newArr; } module.exports = minMaxLengthAverage; diff --git a/easy/find-the-smallest-and-biggest-numbers/code.js b/easy/find-the-smallest-and-biggest-numbers/code.js index da7b4b08..1c17aadb 100644 --- a/easy/find-the-smallest-and-biggest-numbers/code.js +++ b/easy/find-the-smallest-and-biggest-numbers/code.js @@ -1,5 +1,19 @@ function minMax(arr) { - // Your code here. + const newArr = []; + let min = arr[0]; + let max = arr[0]; + for (let i = 0; i < arr.length; i++) { + if (arr[i] > max) { + max = arr[i]; + } + if (arr[i] < min) { + min = arr[i] + } + } + newArr.push(min); + newArr.push(max); + + return newArr; } module.exports = minMax; diff --git a/hard/find-repeating/code.js b/hard/find-repeating/code.js index a5eea26a..48837c41 100644 --- a/hard/find-repeating/code.js +++ b/hard/find-repeating/code.js @@ -1,5 +1,15 @@ function findRepeating(str) { - // Your code here. + const arr = []; + let stack = 0; + const tempArr = []; + for (let i = 0; i < str.length; i++) { + let current = str[i]; + if (stack === 0) { + tempArr.push(str[i]); + } + } + + return arr; } module.exports = findRepeating; diff --git a/hard/find-the-shared-letters-between-two-strings/code.js b/hard/find-the-shared-letters-between-two-strings/code.js index 6f7d3910..60b1849e 100644 --- a/hard/find-the-shared-letters-between-two-strings/code.js +++ b/hard/find-the-shared-letters-between-two-strings/code.js @@ -1,5 +1,16 @@ function sharedLetters(str1, str2) { - // Your code here. + const copies = []; + str1 = str1.toLowerCase(); + str2 = str2.toLowerCase(); + for (let i = 0; i < str1.length; i++) { + if (str2.includes(str1[i]) && !copies.includes(str1[i])) { + copies.push(str1[i]); + } + } + + copies.sort(); + + return copies.join(""); } module.exports = sharedLetters; diff --git a/hard/find-the-shortest-word-s-in-a-sentence/code.js b/hard/find-the-shortest-word-s-in-a-sentence/code.js index 07c236b1..f7913677 100644 --- a/hard/find-the-shortest-word-s-in-a-sentence/code.js +++ b/hard/find-the-shortest-word-s-in-a-sentence/code.js @@ -1,5 +1,21 @@ function findShortestWords(str) { - // Your code here. + const words = str.split(" "); + const wordLength = []; + const shortest = []; + let min = words[0].length; + for (let i = 0; i < words.length; i++) { + wordLength.push(words[i].length); + if (min > words[i].length) min = words[i].length; + } + + for (let i = 0; i < words.length; i++) { + if (words[i].length === min && isNaN(parseInt(words[i]))) { + shortest.push(words[i].toLowerCase()); + } + } + + console.log(words, wordLength, min); + return shortest.sort(); } module.exports = findShortestWords; diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index 08dd005f..290a128b 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -1,5 +1,21 @@ function flipArray(arr) { - // Your code here. + if (!arr[0]) { + return []; + } + const newArr = []; + let columns = arr[0].length; + + if (columns === undefined) { + for (let i = 0; i < arr.length; i++) { + newArr.push([arr[i]]); + } + } else { + for (let i = 0; i < arr.length; i++) { + newArr.push(arr[i][0]); + } + } + + return newArr; } module.exports = flipArray; diff --git a/hard/fridge-poetry/code.js b/hard/fridge-poetry/code.js index d75ccc1c..772866bc 100644 --- a/hard/fridge-poetry/code.js +++ b/hard/fridge-poetry/code.js @@ -1,5 +1,14 @@ function canBuild(s1, s2) { - // Your code here. + const string1 = s1.replace(/ /g, ''); + const string2 = s2.replace(/ /g, ''); + + for (let i = 0; i < s1.length; i++) { + if (!s1.includes(s2[i])) { + return false; + } + } + + return true; } module.exports = canBuild; diff --git a/hard/game-of-thrones-character-titles/code.js b/hard/game-of-thrones-character-titles/code.js index 1ed423ef..39ded621 100644 --- a/hard/game-of-thrones-character-titles/code.js +++ b/hard/game-of-thrones-character-titles/code.js @@ -1,5 +1,15 @@ function correctTitle(str) { - // Your code here. + const words = str.split(" ") + for (let i = 0; i < words.length; i++) { + const currentWord = words[i].toLowerCase(); + if (currentWord === "and" || currentWord === "the" || currentWord === "of" + || currentWord === "in") { + words[i] = currentWord; + } else { + words[i] = currentWord[0].toUpperCase() + currentWord.substring(1, currentWord.length); + } + } + return words.join(" ") } module.exports = correctTitle; diff --git a/hard/gold-distribution/code.js b/hard/gold-distribution/code.js index 9679af60..74477ad7 100644 --- a/hard/gold-distribution/code.js +++ b/hard/gold-distribution/code.js @@ -1,5 +1,16 @@ function piratesKilled(gold, tolerance) { - // Your code here. + let max = 0; + for (let i = 0; i < gold.length; i++) { + if (max < gold[i]) { + max = gold[i] + } + } + for (let i = 0; i < gold.length; i++) { + if (tolerance[i] < (max - gold[i])) { + return true; + } + } + return false; } module.exports = piratesKilled; diff --git a/hard/grocery-store-prices/code.js b/hard/grocery-store-prices/code.js index 22079b39..35acae55 100644 --- a/hard/grocery-store-prices/code.js +++ b/hard/grocery-store-prices/code.js @@ -1,5 +1,11 @@ function getPrices(arr) { - // Your code here. + const prices = []; + for (let i = 0; i < arr.length; i++) { + prices.push(arr[i].match(/\(([^)]+)\)/)[1]) + prices[i] = parseFloat(prices[i].substr(1)); + } + + return prices; } module.exports = getPrices; diff --git a/medium/a-pirate-s-tale/code.js b/medium/a-pirate-s-tale/code.js index e1fd1c4c..d506d36f 100644 --- a/medium/a-pirate-s-tale/code.js +++ b/medium/a-pirate-s-tale/code.js @@ -1,5 +1,17 @@ function numberOfDays(coordinate) { - // Your code here. + let days = 0; + let daysTaken = 0; + for (let i = 0; i < coordinate.length; i++) { + if (coordinate[i] !== 0) { + if (days === 5) { + days = 0; + continue; + } + + days++ + } + } + return daysTaken; } module.exports = numberOfDays; diff --git a/medium/accumulating-product/code.js b/medium/accumulating-product/code.js index f31214c4..c1d28ee1 100644 --- a/medium/accumulating-product/code.js +++ b/medium/accumulating-product/code.js @@ -1,5 +1,11 @@ function accumulatingProduct(arr) { - // Your code here. + let accumulate = 0; + const newArray = []; + for (let i = 0; i < arr.length; i++) { + accumulate *= arr[i]; + newArray.push(accumulate); + } + return newArray; } module.exports = accumulatingProduct; diff --git a/medium/algebra-sequence-boxes/code.js b/medium/algebra-sequence-boxes/code.js index 28b7ccbf..043d0042 100644 --- a/medium/algebra-sequence-boxes/code.js +++ b/medium/algebra-sequence-boxes/code.js @@ -1,5 +1,16 @@ function boxSeq(step) { - // Your code here. + let currentNumber = 0; + if (step === 0) { + return currentNumber; + } + for(let i = 1; i <= step; i++) { + if (i % 2 === 1) { + currentNumber += 3 + } else { + currentNumber -= 1; + } + } + return currentNumber; } module.exports = boxSeq; diff --git a/medium/all-about-strings/code.js b/medium/all-about-strings/code.js index 93aeaa0d..a452924b 100644 --- a/medium/all-about-strings/code.js +++ b/medium/all-about-strings/code.js @@ -1,5 +1,32 @@ function allAboutStrings(str) { - // Your code here. + const arr = []; + const letters = []; + let repeat = false; + arr.push(str.length); + arr.push(str.substring(0, 1)); + arr.push(str.substring(str.length - 1)); + if(str.length % 2 === 1) { + arr.push(str.substr((str.length - 1) / 2, 1)) + } else { + arr.push(str.substr(((str.length - 1) / 2), 2)) + } + + for (let i = 0; i < str.length; i++) { + if (letters.includes(str[i])) { + arr.push(`@ index ${i}`) + repeat = true; + break; + } else { + letters.push(str[i]) + } + } + + if (!repeat) { + arr.push("not found"); + } + + console.log(arr) + return arr; } module.exports = allAboutStrings; diff --git a/medium/alternating-caps/code.js b/medium/alternating-caps/code.js index 0385f3ea..1f6ee319 100644 --- a/medium/alternating-caps/code.js +++ b/medium/alternating-caps/code.js @@ -1,5 +1,14 @@ function alternatingCaps(str) { - // Your code here. + let word = "" + for (let i = 0; i < str.length; i++) { + const letter = str[i]; + if (i % 2 === 0) { + word += letter.toUpperCase(); + } else { + word += letter.toLowerCase(); + } + } + return word; } module.exports = alternatingCaps; diff --git a/medium/alternating-ones-and-zeroes/code.js b/medium/alternating-ones-and-zeroes/code.js index 9bec6fc3..eb7e9869 100644 --- a/medium/alternating-ones-and-zeroes/code.js +++ b/medium/alternating-ones-and-zeroes/code.js @@ -1,5 +1,19 @@ function canAlternate(s) { - // Your code here. + let zeros = 0; + let ones = 0; + for (let i = 0; i < s.length; i++) { + if (s[i] === "0") { + zeros++; + } else { + ones++; + } + } + const difference = zeros - ones; + if (difference > 1 || difference < -1) { + return false; + } else { + return true; + } } module.exports = canAlternate; diff --git a/medium/amazing-alliteration/code.js b/medium/amazing-alliteration/code.js index 6562cca0..90192423 100644 --- a/medium/amazing-alliteration/code.js +++ b/medium/amazing-alliteration/code.js @@ -1,5 +1,20 @@ function alliterationCorrect(sentence) { - // Your code here. + const words = sentence.split(" "); + const letters = []; + let sameLetter = '' + for (let i = 0; i < words.length; i++) { + if (words[i].length > 3) { + const currentLetter = words[i].substr(0, 1).toLowerCase(); + letters.push(currentLetter); + if (sameLetter === '') { + sameLetter = letters[0].toLowerCase(); + } + if (sameLetter !== currentLetter) { + return false; + } + } + } + return true; } module.exports = alliterationCorrect; diff --git a/medium/are-letters-in-the-second-string-present-in-the-first/code.js b/medium/are-letters-in-the-second-string-present-in-the-first/code.js index 65d77376..3855966f 100644 --- a/medium/are-letters-in-the-second-string-present-in-the-first/code.js +++ b/medium/are-letters-in-the-second-string-present-in-the-first/code.js @@ -1,5 +1,12 @@ function letterCheck(arr) { - // Your code here. + const firstWord = arr[0].toLowerCase(); + const secondWord = arr[1].toLowerCase(); + for (let i = 0; i < secondWord.length; i++) { + if (!firstWord.includes(secondWord[i])) { + return false; + } + } + return true; } module.exports = letterCheck; diff --git a/medium/atm-pin-code-validation/code.js b/medium/atm-pin-code-validation/code.js index bcc167b9..65247d64 100644 --- a/medium/atm-pin-code-validation/code.js +++ b/medium/atm-pin-code-validation/code.js @@ -1,5 +1,16 @@ function validatePIN(pin) { - // Your code here. + if (pin.length !== 4 && pin.length !==6) { + return false; + } + const parsed = parseInt(pin); + if (parsed < 1000) { + return false; + } + if (isNaN(parsed)) { + return false + } else { + return true + } } module.exports = validatePIN; diff --git a/medium/automorphic-numbers/code.js b/medium/automorphic-numbers/code.js index 697a5a44..df4ca320 100644 --- a/medium/automorphic-numbers/code.js +++ b/medium/automorphic-numbers/code.js @@ -1,5 +1,15 @@ function isAutomorphic(n) { - // Your code here. + const nString = "" + n; + const nLength = nString.length + const nSquared = n * n; + const nSquaredString = "" + nSquared; + const endPoint = nSquaredString.length-nLength; + const compare = nSquaredString.substr(endPoint, nLength) + if (parseInt(compare) === n) { + return true; + } else { + return false; + } } module.exports = isAutomorphic; diff --git a/medium/barbecue-skewers/code.js b/medium/barbecue-skewers/code.js index 5898cfad..2449c893 100644 --- a/medium/barbecue-skewers/code.js +++ b/medium/barbecue-skewers/code.js @@ -1,5 +1,17 @@ function bbqSkewers(grill) { - // Your code here. + let veggie = 0; + let nonVeggie = 0; + const portions = []; + for (let i = 0; i < grill.length; i ++) { + if (!grill[i].includes("x")) { + veggie++; + } else { + nonVeggie++; + } + } + portions.push(veggie); + portions.push(nonVeggie); + return portions; } module.exports = bbqSkewers; diff --git a/medium/basic-e-mail-validation/code.js b/medium/basic-e-mail-validation/code.js index e813c01b..45a11e13 100644 --- a/medium/basic-e-mail-validation/code.js +++ b/medium/basic-e-mail-validation/code.js @@ -1,5 +1,15 @@ function validateEmail(str) { - // Your code here. + const dotCom = str.substring(str.length - 4, str.length); + if (!str.includes("@")) { + return false; + } + if (str[0] === "@") { + return false; + } + if (dotCom !== ".com") { + return false; + } + return true; } module.exports = validateEmail; diff --git a/medium/bird-names-to-four-letter-bird-codes/code.js b/medium/bird-names-to-four-letter-bird-codes/code.js index 4bb03e8c..5b49b76c 100644 --- a/medium/bird-names-to-four-letter-bird-codes/code.js +++ b/medium/bird-names-to-four-letter-bird-codes/code.js @@ -1,5 +1,23 @@ function birdCode(arr) { - // Your code here. + const newArray = []; + for (let i = 0; i < arr.length; i++) { + const birdName = arr[i].replace("-", " ").split(" "); + if (birdName.length === 1) { + newArray.push(birdName[0].substr(0, 4).toUpperCase()); + } else if (birdName.length === 2) { + const birdAbbrev = birdName[0].substr(0, 2) + birdName[1].substr(0, 2); + newArray.push(birdAbbrev.toUpperCase()); + } else if (birdName.length === 3) { + const birdAbbrev = birdName[0].substr(0, 1) + birdName[1].substr(0, 1) + + birdName[2].substr(0, 2); + newArray.push(birdAbbrev.toUpperCase()); + } else if (birdName.length === 4) { + const birdAbbrev = birdName[0].substr(0, 1) + birdName[1].substr(0, 1) + + birdName[2].substr(0, 1) + birdName[3].substr(0, 1); + newArray.push(birdAbbrev.toUpperCase()); + } + } + return newArray; } module.exports = birdCode; diff --git a/medium/blackjack/code.js b/medium/blackjack/code.js index 081200b7..7973c91f 100644 --- a/medium/blackjack/code.js +++ b/medium/blackjack/code.js @@ -1,5 +1,26 @@ function overTwentyOne(cards) { - // Your code here. + let sum = 0; + let hasAce = false; + for (let i = 0; i < cards.length; i++) { + if (cards[i] === "J" || cards[i] === "Q" || cards[i] === "K") { + sum += 10; + } else if (cards[i] === "A") { + hasAce = true; + } else { + sum += cards[i]; + } + } + if(hasAce) { + if ((sum + 11) > 21) { + sum += 1; + } else { + sum +=11; + } + } + if (sum > 21) { + return true; + } + return false; } module.exports = overTwentyOne; diff --git a/medium/box-completely-filled/code.js b/medium/box-completely-filled/code.js index f45bff51..1fdafa11 100644 --- a/medium/box-completely-filled/code.js +++ b/medium/box-completely-filled/code.js @@ -1,5 +1,14 @@ function completelyFilled(arr) { - // Your code here. + const boxLength = arr[0].length; + if (boxLength < 3) { + return true; + } + for (let i = 0; i < arr.length; i++) { + if (arr[i].includes(" ")) { + return false; + } + } + return true; } module.exports = completelyFilled; diff --git a/medium/calculate-the-difference-between-two-numbers/code.js b/medium/calculate-the-difference-between-two-numbers/code.js index 709bca91..ffa2136f 100644 --- a/medium/calculate-the-difference-between-two-numbers/code.js +++ b/medium/calculate-the-difference-between-two-numbers/code.js @@ -1,5 +1,13 @@ function percentDiff(num1, num2) { - // Your code here. + let difference = 0; + if (num1 > num2) { + difference = num1 - num2; + } else { + difference = num2 - num1; + } + const average = (num1 + num2) / 2 + const percent = ((difference / average) * 100).toFixed(1); + return (parseFloat(percent)) } module.exports = percentDiff; diff --git a/medium/calculate-the-shortest-distance-between-2-points/code.js b/medium/calculate-the-shortest-distance-between-2-points/code.js index 836d13ad..fc088ed6 100644 --- a/medium/calculate-the-shortest-distance-between-2-points/code.js +++ b/medium/calculate-the-shortest-distance-between-2-points/code.js @@ -1,5 +1,14 @@ function shortestDistance(str) { - // Your code here. + const arr = str.split(","); + for(let i = 0; i < arr.length; i++) { + arr[i] = parseInt(arr[i]); + } + const xDiffer = arr[2] - arr[0]; + const yDiffer = arr[3] - arr[1]; + + const differ = xDiffer * xDiffer + yDiffer * yDiffer; + + return parseFloat(Math.sqrt(differ).toFixed(2)); } module.exports = shortestDistance; diff --git a/medium/capitalize-the-first-letter-of-each-word/code.js b/medium/capitalize-the-first-letter-of-each-word/code.js index 87c4a4a9..40036f1e 100644 --- a/medium/capitalize-the-first-letter-of-each-word/code.js +++ b/medium/capitalize-the-first-letter-of-each-word/code.js @@ -1,5 +1,10 @@ function makeTitle(str) { - // Your code here. + const arr = str.split(" "); + for (let i = 0; i < arr.length; i++) { + arr[i] = arr[i][0].toUpperCase() + arr[i].substring(1) + } + const newStr = arr.join(" ") + return newStr; } module.exports = makeTitle; diff --git a/medium/capitalize-the-names/code.js b/medium/capitalize-the-names/code.js index fb606828..7c844971 100644 --- a/medium/capitalize-the-names/code.js +++ b/medium/capitalize-the-names/code.js @@ -1,5 +1,10 @@ function capMe(arr) { - // Your code here. + const newArr = []; + for (let i = 0; i < arr.length; i++) { + const word = arr[i][0].toUpperCase() + arr[i].substring(1).toLowerCase(); + newArr.push(word) + } + return newArr; } module.exports = capMe; diff --git a/medium/check-for-anagrams/code.js b/medium/check-for-anagrams/code.js index da2955bb..8509ee4b 100644 --- a/medium/check-for-anagrams/code.js +++ b/medium/check-for-anagrams/code.js @@ -1,5 +1,19 @@ function isAnagram(s1, s2) { - // Your code here. + const word1 = s1.toLowerCase().split(""); + const word2 = s2.toLowerCase().split(""); + for(let i = 0; i < word1.length; i++) { + if (!word2.includes(word1[i])) { + return false; + } else { + const index = word2.indexOf(word1[i]) + word2.splice(index, 1) + } + } + if (word2.length === 0) { + return true; + } else { + return false; + } } module.exports = isAnagram; diff --git a/medium/check-if-a-number-is-prime/code.js b/medium/check-if-a-number-is-prime/code.js index ffdcef1c..73a70957 100644 --- a/medium/check-if-a-number-is-prime/code.js +++ b/medium/check-if-a-number-is-prime/code.js @@ -1,5 +1,13 @@ function isPrime(num) { - // Your code here. + if (num === 1) { + return false; + } + for (let i = 2; i < num; i++) { + if(num % i === 0) { + return false; + } + } + return true; } module.exports = isPrime; diff --git a/medium/check-if-one-array-is-subset-of-another/code.js b/medium/check-if-one-array-is-subset-of-another/code.js index e05aa8bd..a7e09f8b 100644 --- a/medium/check-if-one-array-is-subset-of-another/code.js +++ b/medium/check-if-one-array-is-subset-of-another/code.js @@ -1,5 +1,13 @@ function subset(arr1, arr2) { - // Your code here. + for(let i = 0; i < arr1.length; i++) { + if (!arr2.includes(arr1[i])) { + return false; + } else { + const index = arr2.indexOf(arr1[i]) + arr2.splice(index, 1) + } + } + return true; } module.exports = subset; diff --git a/medium/check-if-the-string-is-a-palindrome/code.js b/medium/check-if-the-string-is-a-palindrome/code.js index be712559..f80bf82c 100644 --- a/medium/check-if-the-string-is-a-palindrome/code.js +++ b/medium/check-if-the-string-is-a-palindrome/code.js @@ -1,5 +1,12 @@ function isPalindrome(str) { - // Your code here. + let newStr = str.replace(/ /g, "").replace(/,/g, "").replace(/[^\w]/g, "").toLowerCase(); + const strLength = newStr.length - 1; + for (let i = 0; i < newStr.length; i++) { + if(newStr[i] !== newStr[strLength - i]) { + return false; + } + } + return true; } module.exports = isPalindrome; diff --git a/medium/chocolate-dilemma/code.js b/medium/chocolate-dilemma/code.js index 28b110fa..385efb24 100644 --- a/medium/chocolate-dilemma/code.js +++ b/medium/chocolate-dilemma/code.js @@ -1,5 +1,18 @@ function testFairness(agatha, bertha) { - // Your code here. + let agathaChoco = 0; + let berthaChoco = 0; + for (let i = 0; i < agatha.length; i++) { + agathaChoco += agatha[i][0] * agatha[i][1]; + } + for (let i = 0; i < bertha.length; i++) { + berthaChoco += bertha[i][0] * bertha[i][1]; + } + + if (agathaChoco === berthaChoco) { + return true; + } else { + return false; + } } module.exports = testFairness; diff --git a/medium/christmas-tree/code.js b/medium/christmas-tree/code.js index 91af3656..206a3ec9 100644 --- a/medium/christmas-tree/code.js +++ b/medium/christmas-tree/code.js @@ -1,5 +1,23 @@ function tree(h) { - // Your code here. + const cTree = []; + for (let i = 1; i < h + 1; i++) { + let layer = ""; + for (let k = 0; k < h - i ; k++) { + layer += " "; + } + for (let j = 0; j < i; j++) { + if (j === 0) { + layer += "#"; + } else { + layer += "##" + } + } + for (let k = 0; k < h - i; k++) { + layer += " "; + } + cTree.push(layer); + } + return cTree; } module.exports = tree; diff --git a/medium/compounding-letters/code.js b/medium/compounding-letters/code.js index 6db4c4bf..9b17cc80 100644 --- a/medium/compounding-letters/code.js +++ b/medium/compounding-letters/code.js @@ -1,5 +1,19 @@ function accum(str) { - // Your code here. + const arr = str.split(""); + let word = ""; + for (let i = 0; i < arr.length; i++) { + for( let k = 0; k <= i; k++) { + if ( k === 0) { + word += arr[i].toUpperCase(); + } else { + word += arr[i].toLowerCase(); + } + } + if(i !== arr.length - 1) { + word += "-" + } + } + return word; } module.exports = accum; diff --git a/medium/convenience-store/code.js b/medium/convenience-store/code.js index a837e61a..481f879c 100644 --- a/medium/convenience-store/code.js +++ b/medium/convenience-store/code.js @@ -1,5 +1,21 @@ function changeEnough(change, amountDue) { - // Your code here. + let totalChange = 0; + for (let i = 0; i < change.length; i++) { + if (i === 0) { + totalChange += change[i] * .25; + } else if (i === 1) { + totalChange += change[i] * .10; + } else if (i === 2) { + totalChange += change[i] * .05; + } else { + totalChange += change[i] * .01; + } + } + if (totalChange >= amountDue) { + return true; + } else { + return false; + } } module.exports = changeEnough; diff --git a/medium/convert-key-values-in-an-object-to-array/code.js b/medium/convert-key-values-in-an-object-to-array/code.js index 50658430..a6b0443e 100644 --- a/medium/convert-key-values-in-an-object-to-array/code.js +++ b/medium/convert-key-values-in-an-object-to-array/code.js @@ -1,5 +1,6 @@ function objectToArray(obj) { - // Your code here. + const entries = Object.entries(obj); + return entries; } module.exports = objectToArray; diff --git a/medium/convert-zero-and-one-text-to-1-and-0/code.js b/medium/convert-zero-and-one-text-to-1-and-0/code.js index 7e13c767..68abe3e3 100644 --- a/medium/convert-zero-and-one-text-to-1-and-0/code.js +++ b/medium/convert-zero-and-one-text-to-1-and-0/code.js @@ -1,5 +1,22 @@ function textToNumberBinary(str) { - // Your code here. + let binary = ""; + const words = str.split(" ") + for (let i = 0; i < words.length; i++) { + const match = words[i].toLowerCase(); + if (match === "zero") { + binary += 0; + } else if (match === "ten") { + binary += 10; + } else { + binary += 1; + } + } + console.log(binary, typeof(binary), binary.length) + if (binary.length % 8 === 0) { + return binary; + } else { + return ""; + } } module.exports = textToNumberBinary; diff --git a/medium/count-letters-in-a-word-search/code.js b/medium/count-letters-in-a-word-search/code.js index 7661d0f9..372feea0 100644 --- a/medium/count-letters-in-a-word-search/code.js +++ b/medium/count-letters-in-a-word-search/code.js @@ -1,5 +1,13 @@ function letterCounter(arr, letter) { - // Your code here. + let count = 0; + for (let i = 0; i < arr.length; i++) { + for (let k = 0; k < arr[i].length; k++) { + if (arr[i][k] === letter) { + count ++; + } + } + } + return count; } module.exports = letterCounter; diff --git a/medium/count-ones-in-binary-representation-of-integer/code.js b/medium/count-ones-in-binary-representation-of-integer/code.js index 5eec5af1..a0622779 100644 --- a/medium/count-ones-in-binary-representation-of-integer/code.js +++ b/medium/count-ones-in-binary-representation-of-integer/code.js @@ -1,5 +1,13 @@ function countOnes(i) { - // Your code here. + let countOne = 0; + const binary = i.toString(2) + "" + const binaryNumber = binary.split(""); + for(let i = 0; i < binaryNumber.length; i++) { + if (binaryNumber[i] === "1") { + countOne++; + } + } + return countOne } module.exports = countOnes; diff --git a/medium/count-the-letters-and-digits/code.js b/medium/count-the-letters-and-digits/code.js index 55445d54..61793392 100644 --- a/medium/count-the-letters-and-digits/code.js +++ b/medium/count-the-letters-and-digits/code.js @@ -1,5 +1,20 @@ function countAll(str) { - // Your code here. + const letters = str.split("") + const count = {} + let lettersCount = 0; + let numberCount = 0; + for (let i = 0; i < letters.length; i++) { + if (isNaN(letters[i])) { + lettersCount++; + } else if (letters[i] === " ") { + continue; + } else { + numberCount++ + } + } + count.LETTERS = lettersCount; + count.DIGITS = numberCount; + return count; } module.exports = countAll; diff --git a/medium/count-the-number-of-duplicate-characters/code.js b/medium/count-the-number-of-duplicate-characters/code.js index 921ca2cf..51fc05d9 100644 --- a/medium/count-the-number-of-duplicate-characters/code.js +++ b/medium/count-the-number-of-duplicate-characters/code.js @@ -1,5 +1,19 @@ function duplicateCount(str) { - // Your code here. + const countDup = {}; + let count = 0; + for (let i = 0; i < str.length; i++) { + if (countDup.hasOwnProperty(str[i])) { + countDup[str[i]]++; + } else { + countDup[str[i]] = 1; + } + } + for (const property in countDup) { + if (countDup[property] > 1) { + count++; + } + } + return count; } module.exports = duplicateCount;