diff --git a/easy/index-shuffle/code.js b/easy/index-shuffle/code.js index fabc3dd0..5fc6bc96 100644 --- a/easy/index-shuffle/code.js +++ b/easy/index-shuffle/code.js @@ -1,5 +1,14 @@ function indexShuffle(str) { - // Your code here. + let evenIndex = ""; + let oddIndex = ""; + for (let i = 0; i < str.length; i++) { + if (i % 2 === 0) { + evenIndex += str[i]; + } else { + oddIndex += str[i]; + } + } + return evenIndex + oddIndex; } module.exports = indexShuffle; diff --git a/easy/join-two-portions-of-a-path/code.js b/easy/join-two-portions-of-a-path/code.js index fd371eba..f9b9033e 100644 --- a/easy/join-two-portions-of-a-path/code.js +++ b/easy/join-two-portions-of-a-path/code.js @@ -1,5 +1,11 @@ function joinPath(portion1, portion2) { - // Your code here. + if (portion1[portion1.length - 1] === "/" && portion2[0] === "/") { + return portion1.substr(0, portion1.length - 1) + portion2; + } else if (portion1[portion1.length - 1] === "/" || portion2[0] === "/") { + return portion1 + portion2; + } else { + return portion1 + "/" + portion2; + } } module.exports = joinPath; diff --git a/easy/largest-swap/code.js b/easy/largest-swap/code.js index b9443539..55ed7dad 100644 --- a/easy/largest-swap/code.js +++ b/easy/largest-swap/code.js @@ -1,5 +1,11 @@ function largestSwap(num) { - // Your code here. + const oneDigit = num % 10; + const tenDigit = Math.floor(num / 10); + if (oneDigit > tenDigit) { + return false; + } else { + return true; + } } module.exports = largestSwap; diff --git a/easy/pi-to-n-decimal-places/code.js b/easy/pi-to-n-decimal-places/code.js index 56cfb017..cc7b3417 100644 --- a/easy/pi-to-n-decimal-places/code.js +++ b/easy/pi-to-n-decimal-places/code.js @@ -1,5 +1,6 @@ function myPi(n) { - // Your code here. + const pi = 3.141592653589793; + return parseFloat(pi.toFixed(n)); } module.exports = myPi; diff --git a/easy/puzzle-pieces/code.js b/easy/puzzle-pieces/code.js index 5014d240..2b793906 100644 --- a/easy/puzzle-pieces/code.js +++ b/easy/puzzle-pieces/code.js @@ -1,5 +1,15 @@ function puzzlePieces(a1, a2) { - // Your code here. + if (a1.length !== a2.length) { + return false; + } + const fit = a1[0] + a2[0]; + for (let i = 1; i < a1.length; i++) { + const currentPiece = a1[i] + a2[i]; + if (fit !== currentPiece) { + return false; + } + } + return true; } module.exports = puzzlePieces; diff --git a/easy/repeating-letters/code.js b/easy/repeating-letters/code.js index af356167..685e8f45 100644 --- a/easy/repeating-letters/code.js +++ b/easy/repeating-letters/code.js @@ -1,5 +1,10 @@ function doubleChar(str) { - // Your code here. + let doubleStr = ""; + for (let i = 0; i < str.length; i++) { + doubleStr += str[i]; + doubleStr += str[i]; + } + return doubleStr; } module.exports = doubleChar; diff --git a/easy/return-the-factorial/code.js b/easy/return-the-factorial/code.js index 0b792944..f50ebe45 100644 --- a/easy/return-the-factorial/code.js +++ b/easy/return-the-factorial/code.js @@ -1,5 +1,9 @@ function factorial(int) { - // Your code here. + let total = 1; + for (let i = 1; i < int; i++) { + total *= i; + } + return total; } module.exports = factorial; diff --git a/easy/return-the-highest-and-lowest-numbers/code.js b/easy/return-the-highest-and-lowest-numbers/code.js index 519eb6ef..08a0901d 100644 --- a/easy/return-the-highest-and-lowest-numbers/code.js +++ b/easy/return-the-highest-and-lowest-numbers/code.js @@ -1,5 +1,14 @@ function highLow(str) { - // Your code here. + const numbers = str.split(" ") + let highest = parseInt(numbers[0]) + let lowest = parseInt(numbers[0]) + for (let i = 0; i < numbers.length; i++) { + if (highest < parseInt(numbers[i])) { + highest = parseInt(numbers[i]); + } + if (lowest > parseInt(numbers[i])) lowest = parseInt(numbers[i]) + } + return highest + " " + lowest; } module.exports = highLow; diff --git a/easy/secret-society/code.js b/easy/secret-society/code.js index 330e7aae..c4c5826c 100644 --- a/easy/secret-society/code.js +++ b/easy/secret-society/code.js @@ -1,5 +1,16 @@ function societyName(friends) { - // Your code here. + let name = ""; + const initals = []; + for (let i = 0; i < friends.length; i++) { + initals.push(friends[i][0]); + } + initals.sort(); + + for (let i = 0; i < initals.length; i++) { + name += initals[i] + } + + return name; } module.exports = societyName; diff --git a/easy/snail-race/code.js b/easy/snail-race/code.js index 6263bcd0..0e9b9acd 100644 --- a/easy/snail-race/code.js +++ b/easy/snail-race/code.js @@ -1,5 +1,21 @@ function mauriceWins(mSnails, sSnails) { - // Your code here. + let wins = 0; + + if (mSnails[0] > sSnails[2]) { + wins++; + } + if (mSnails[1] > sSnails[0]) { + wins++ + } + if (mSnails[2] > sSnails[1]) { + wins++ + } + + if (wins >= 2) { + return true; + } else { + return false; + } } module.exports = mauriceWins; diff --git a/easy/spelling-it-out/code.js b/easy/spelling-it-out/code.js index 796eb731..be22002d 100644 --- a/easy/spelling-it-out/code.js +++ b/easy/spelling-it-out/code.js @@ -1,5 +1,10 @@ function spelling(str) { - // Your code here. + const arr = []; + for (let i = 0; i < str.length; i++) { + arr.push(str.substr(0, i)) + } + + return arr; } module.exports = spelling; diff --git a/easy/sum-of-all-evens-in-a-matrix/code.js b/easy/sum-of-all-evens-in-a-matrix/code.js index 3055b71f..c7df7189 100644 --- a/easy/sum-of-all-evens-in-a-matrix/code.js +++ b/easy/sum-of-all-evens-in-a-matrix/code.js @@ -1,5 +1,14 @@ function sumOfEvens(arr) { - // Your code here. + let evenTotal = 0; + for (let i = 0; i < arr.length; i++) { + for (let k = 0; k < arr[i].length; k++) { + if (arr[i][k] % 2 === 0) { + evenTotal += arr[i][k]; + } + } + } + + return evenTotal; } module.exports = sumOfEvens; diff --git a/easy/sum-of-cubes/code.js b/easy/sum-of-cubes/code.js index 0d45b3c9..85dbbb88 100644 --- a/easy/sum-of-cubes/code.js +++ b/easy/sum-of-cubes/code.js @@ -1,5 +1,11 @@ function sumOfCubes(nums) { - // Your code here. + let total = 0; + for (let i = 0; i < nums.length; i++) { + let cube = Math.cube(nums[i]) + total += cube; + } + + return total; } module.exports = sumOfCubes; diff --git a/easy/transform-into-an-array-with-no-duplicates/code.js b/easy/transform-into-an-array-with-no-duplicates/code.js index ff4ab7ca..538091c7 100644 --- a/easy/transform-into-an-array-with-no-duplicates/code.js +++ b/easy/transform-into-an-array-with-no-duplicates/code.js @@ -1,5 +1,14 @@ function set(arr) { - // Your code here. + const obj = {}; + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if (!obj.hasOwnProperty(arr[i])) { + obj[arr[i]] = arr[i] + newArr.push(arr[i]) + } + } + + return newArr; } module.exports = set; diff --git a/easy/transform-upvotes/code.js b/easy/transform-upvotes/code.js index 06cadf92..73b7bd81 100644 --- a/easy/transform-upvotes/code.js +++ b/easy/transform-upvotes/code.js @@ -1,5 +1,16 @@ function transformUpvotes(str) { - // Your code here. + const arr = []; + const strArr = str.split(" "); + for(let i = 0; i < strArr.length; i++) { + if (strArr[i].indexOf("k") !== -1) { + let newNum = strArr[i].replace(/k/g, ""); + let convertNum = parseFloat(newNum); + let trueNumber = convertNum * 1000; + arr.push(trueNumber); + } + } + + return arr; } module.exports = transformUpvotes; diff --git a/easy/write-a-function-to-calculate-the-factorial-of-a-number/code.js b/easy/write-a-function-to-calculate-the-factorial-of-a-number/code.js index 98c363ee..ba7afb65 100644 --- a/easy/write-a-function-to-calculate-the-factorial-of-a-number/code.js +++ b/easy/write-a-function-to-calculate-the-factorial-of-a-number/code.js @@ -1,5 +1,10 @@ function factorial(x) { - // Your code here. + let total = 1; + for (let i = 2; i < x; i++) { + total *= i; + } + + return total; } module.exports = factorial; diff --git a/hard/all-pairs-that-sum-to-target/code.js b/hard/all-pairs-that-sum-to-target/code.js index d41667ac..6182efc4 100644 --- a/hard/all-pairs-that-sum-to-target/code.js +++ b/hard/all-pairs-that-sum-to-target/code.js @@ -1,5 +1,21 @@ function allPairs(arr, target) { - // Your code here. + const newArr = [] + for (let i = 0; i < arr.length - 1; i++) { + for (let k = i + 1; k < arr.length ; k++) { + if (arr[i] + arr[k] === target) { + const tempArr = []; + if (arr[i] > arr[k]) { + tempArr.push(arr[k]); + tempArr.push(arr[i]); + } else { + tempArr.push(arr[i]); + tempArr.push(arr[k]); + } + newArr.push(tempArr); + } + } + } + return newArr; } module.exports = allPairs; diff --git a/hard/beginning-and-end-pairs/code.js b/hard/beginning-and-end-pairs/code.js index 6dc6139a..e1e15779 100644 --- a/hard/beginning-and-end-pairs/code.js +++ b/hard/beginning-and-end-pairs/code.js @@ -1,5 +1,21 @@ function pairs(arr) { - // Your code here. + const newArr = []; + let arrHalf = null; + if (arr.length % 2 === 0) { + arrHalf = arr.length / 2; + } else { + arrHalf = (arr.length - 1) / 2; + } + + for (let i = 0; i < arrHalf; i++) { + const tempArr = []; + tempArr.push(arr[i]); + tempArr.push(arr[arr.length - (i + 1)]) + newArr.push(tempArr); + } + return newArr; } + + module.exports = pairs; 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; diff --git a/medium/cumulative-array-sum/code.js b/medium/cumulative-array-sum/code.js index 683221ec..f40b7858 100644 --- a/medium/cumulative-array-sum/code.js +++ b/medium/cumulative-array-sum/code.js @@ -1,5 +1,11 @@ function cumulativeSum(arr) { - // Your code here. + let cumulative = 0; + const cumulativeArr = []; + for (let i = 0; i < arr.length; i++) { + cumulative += arr[i]; + cumulativeArr.push(cumulative) + } + return cumulativeArr; } module.exports = cumulativeSum; diff --git a/medium/delete-occurrences-of-extra-elements-in-an-array/code.js b/medium/delete-occurrences-of-extra-elements-in-an-array/code.js index 637ad2e1..e5ad7661 100644 --- a/medium/delete-occurrences-of-extra-elements-in-an-array/code.js +++ b/medium/delete-occurrences-of-extra-elements-in-an-array/code.js @@ -1,5 +1,18 @@ function deleteOccurrences(arr, num) { - // Your code here. + const obj = {}; + const newArr = []; + for (let i = 0; i < arr.length; i++) { + if(obj.hasOwnProperty(arr[i])) { + obj[arr[i]]++; + + } else { + obj[arr[i]] = 1; + } + if (obj[arr[i]] <= num) { + newArr.push(arr[i]) + } + } + return newArr; } module.exports = deleteOccurrences; diff --git a/medium/digit-distance/code.js b/medium/digit-distance/code.js index b8fdb958..72a0def5 100644 --- a/medium/digit-distance/code.js +++ b/medium/digit-distance/code.js @@ -1,5 +1,15 @@ function digitDistance(num1, num2) { - // Your code here. + const str1 = num1 + ""; + const str2 = num2 + ""; + let distance = 0; + for (let i = 0; i < str1.length; i++) { + let difference = parseInt(str1[i]) - parseInt(str2[i]); + if (difference < 0) { + difference *= -1; + } + distance += difference; + } + return distance; } module.exports = digitDistance; diff --git a/medium/divide-array-into-chunks/code.js b/medium/divide-array-into-chunks/code.js index e84abf0f..d2e30d25 100644 --- a/medium/divide-array-into-chunks/code.js +++ b/medium/divide-array-into-chunks/code.js @@ -1,5 +1,16 @@ function chunkify(arr, size) { - // Your code here. + const newArr = []; + while (arr.length !== 0) { + const currentArr = []; + for (let i = 0; i < size; i++) { + if (arr.length === 0) { + break; + } + currentArr.push(arr.shift()); + } + newArr.push(currentArr) + } + return newArr; } module.exports = chunkify; diff --git a/medium/double-letters/code.js b/medium/double-letters/code.js index 93b9b7db..3be4f8a8 100644 --- a/medium/double-letters/code.js +++ b/medium/double-letters/code.js @@ -1,5 +1,12 @@ function doubleLetters(word) { - // Your code here. + for (let i = 0; i < word.length; i++) { + if (i === word.length - 1) { + return false; + } + if (word[i] === word[i + 1]) { + return true; + } + } } module.exports = doubleLetters; diff --git a/medium/equality-of-3-values/code.js b/medium/equality-of-3-values/code.js index ce0c8185..a7aaedeb 100644 --- a/medium/equality-of-3-values/code.js +++ b/medium/equality-of-3-values/code.js @@ -1,5 +1,18 @@ function equal(a, b, c) { - // Your code here. + if (a === b) { + if (a === c) { + return 3; + } else { + return 2; + } + } else { + if (a === c) { + return 2; + } else if (b === c) { + return 2; + } + } + return 0; } module.exports = equal; diff --git a/medium/factorize-a-number/code.js b/medium/factorize-a-number/code.js index 7eb5f693..cd29bf87 100644 --- a/medium/factorize-a-number/code.js +++ b/medium/factorize-a-number/code.js @@ -1,5 +1,11 @@ function factorize(num) { - // Your code here. + const factors = []; + for (let i = 1; i <= num; i++) { + if (num % i === 0) { + factors.push(i); + } + } + return factors; } module.exports = factorize; diff --git a/medium/fix-the-error-value-vs-reference-types/code.js b/medium/fix-the-error-value-vs-reference-types/code.js index ddb2b7e2..2770202b 100644 --- a/medium/fix-the-error-value-vs-reference-types/code.js +++ b/medium/fix-the-error-value-vs-reference-types/code.js @@ -1,5 +1,10 @@ function checkEquals(arr1, arr2) { - // Your code here. + for (let i = 0; i < arr1.length; i++) { + if (arr1[i] !== arr2[i]) { + return false; + } + } + return true; } module.exports = checkEquals; diff --git a/medium/flash-cards/code.js b/medium/flash-cards/code.js index 8c2e7b58..8c1d06bc 100644 --- a/medium/flash-cards/code.js +++ b/medium/flash-cards/code.js @@ -1,5 +1,19 @@ function flash([num1, op, num2]) { - // Your code here. + switch (op) { + case "+": + return num1 + num2; + case "-": + return num1 - num2; + case "x": + return num1 * num2; + case "/": + if(num2 === 0) { + return undefined; + } + return parseFloat((num1 / num2).toFixed(2)); + default: + return undefined; + } } module.exports = flash; diff --git a/medium/format-number-with-comma-s-separating-thousands/code.js b/medium/format-number-with-comma-s-separating-thousands/code.js index be8e9a2a..262edb74 100644 --- a/medium/format-number-with-comma-s-separating-thousands/code.js +++ b/medium/format-number-with-comma-s-separating-thousands/code.js @@ -1,5 +1,11 @@ function formatNum(num) { - // Your code here. + let number = (num + "").split(""); + console.log(number) + for (let i = number.length - 1; i > 0; i - 3) { + number.splice(i, ","); + } + const result = number.join("") + return result; } module.exports = formatNum; diff --git a/medium/frequency-distribution/code.js b/medium/frequency-distribution/code.js index fcdb5fb7..1e55869a 100644 --- a/medium/frequency-distribution/code.js +++ b/medium/frequency-distribution/code.js @@ -1,5 +1,13 @@ function getFrequencies(arr) { - // Your code here. + const obj = {}; +for (let i = 0; i < arr.length; i++) { + if (obj.hasOwnProperty(arr[i])) { + obj[arr[i]]++; + } else { + obj[arr[i]] = 1; + } +} +return obj; } module.exports = getFrequencies; diff --git a/medium/generate-n-size-combinations-from-an-array/code.js b/medium/generate-n-size-combinations-from-an-array/code.js index e58e81f6..0a4807a1 100644 --- a/medium/generate-n-size-combinations-from-an-array/code.js +++ b/medium/generate-n-size-combinations-from-an-array/code.js @@ -1,5 +1,23 @@ function combo(arr, n) { - // Your code here. + const newArr = []; + if (arr.length < n) { + return newArr; + } + if (n === 0) { + newArr.push([]); + return newArr; + } + for(let i = 0; i < arr.length - 1; i++) { + for (let k = i + 1; k < arr.length; k++) { + const tempArr = [arr[i]]; + tempArr.push(arr[k]); + newArr.push(tempArr); + } + } + + console.log(newArr) + + return newArr; } module.exports = combo; diff --git a/medium/hitting-the-jackpot/code.js b/medium/hitting-the-jackpot/code.js index eb950f14..3338f6e8 100644 --- a/medium/hitting-the-jackpot/code.js +++ b/medium/hitting-the-jackpot/code.js @@ -1,5 +1,14 @@ function testJackpot(result) { - // Your code here. + if (result[0] !== result[1]) { + return false; + } + if (result[0] !== result[2]) { + return false; + } + if (result[0] !== result[3]) { + return false; + } + return true; } module.exports = testJackpot; diff --git a/medium/identical-subarrays/code.js b/medium/identical-subarrays/code.js index 55481381..805c74ce 100644 --- a/medium/identical-subarrays/code.js +++ b/medium/identical-subarrays/code.js @@ -1,5 +1,21 @@ function countIdentical(arr) { - // Your code here. + let counter = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length === 1) { + counter++; + } else { + let equal = true; + for (let k = 1; k < arr[i].length; k++) { + if (arr[i][0] !== arr[i][k]) { + equal = false; + } + } + if (equal) { + counter++; + } + } + return counter; + } } module.exports = countIdentical; diff --git a/medium/index-multiplier/code.js b/medium/index-multiplier/code.js index e863849c..72ae16b3 100644 --- a/medium/index-multiplier/code.js +++ b/medium/index-multiplier/code.js @@ -1,5 +1,9 @@ function indexMultiplier(arr) { - // Your code here. + let total = 0; + for (let i = 0; i < arr.length; i++) { + total += (i * arr[i]) + } + return total; } module.exports = indexMultiplier;