From 968cd6c489130287ee0fd967c3916ae6000eeb53 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 12 Aug 2020 10:24:41 -0700 Subject: [PATCH 01/64] inital pirate files --- medium/a-pirate-s-tale/code.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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; From a23c2fbc26ac857eeb240492e2b96cf39576fd28 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 16 Aug 2020 10:24:21 -0700 Subject: [PATCH 02/64] accumlate product complete --- medium/accumulating-product/code.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; From e1ccc68720bad6c70910040782a6c193ceb27300 Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 17 Aug 2020 10:49:00 -0700 Subject: [PATCH 03/64] algrebra sequence exercise complete --- medium/algebra-sequence-boxes/code.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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; From 30bfe6cf2741684d4e3116d93fdc8037427dbde0 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 18 Aug 2020 11:45:04 -0700 Subject: [PATCH 04/64] all about string exercise complete --- medium/all-about-strings/code.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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; From eb1c46273663a947e6208bcb1c578d4e7d607562 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 19 Aug 2020 09:31:38 -0700 Subject: [PATCH 05/64] alternating cap ex complete --- medium/alternating-caps/code.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; From 96c2a39d5ea6b1a9d8dc221930a3b7151a49ea14 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 20 Aug 2020 09:49:55 -0700 Subject: [PATCH 06/64] alternate zero and ones complete --- medium/alternating-ones-and-zeroes/code.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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; From 3a2f6b5c744b9c647ce64ffca8fa154214bd6121 Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 21 Aug 2020 09:45:01 -0700 Subject: [PATCH 07/64] alliteration complete --- medium/amazing-alliteration/code.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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; From f62ba67dbf64f11b6e51c00bc4b74c56ee1be90b Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 22 Aug 2020 10:37:34 -0700 Subject: [PATCH 08/64] are letters in second string present in first complete --- .../code.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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; From 16aad2770b0063b72bea2d05c341c08840bb4dbd Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 23 Aug 2020 12:16:02 -0700 Subject: [PATCH 09/64] atm exercise done --- medium/atm-pin-code-validation/code.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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; From f366c7df1e09ecf7593c791787b76b85f3fbf7de Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 24 Aug 2020 11:57:22 -0700 Subject: [PATCH 10/64] automorphic number complete --- medium/automorphic-numbers/code.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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; From e46ecc5d4b96944a97cbeb26a9d13bcf5cdb85f4 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 25 Aug 2020 09:16:57 -0700 Subject: [PATCH 11/64] barbecue challenge complete --- medium/barbecue-skewers/code.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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; From d683341852fa5e9f41d6e0cabc426880e2194060 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 26 Aug 2020 09:16:34 -0700 Subject: [PATCH 12/64] basic email validation complete --- medium/basic-e-mail-validation/code.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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; From 78d33ba257c85763f897496bc74213532a380f91 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 27 Aug 2020 09:00:14 -0700 Subject: [PATCH 13/64] bird names abbrev complete --- .../code.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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; From 2fa375b2581154e46d30eacec74cf9f77d7b892e Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 28 Aug 2020 09:39:35 -0700 Subject: [PATCH 14/64] blackjack exercise complete --- medium/blackjack/code.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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; From 419f2364add1e3f743a05f89c7da478bcbf77167 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 29 Aug 2020 01:55:34 -0700 Subject: [PATCH 15/64] box exercise complete --- medium/box-completely-filled/code.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; From 84b066684cf8b40ae74bf7c72399800c81c3ceff Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 30 Aug 2020 09:42:45 -0700 Subject: [PATCH 16/64] calculate the difference complete --- .../code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From 517311fd95cb0cca04afe75e0630eb19c390675b Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 31 Aug 2020 11:25:26 -0700 Subject: [PATCH 17/64] calculate distance complete --- .../code.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; From c362b8b7a8bc884cdd8cfd31fe8b4db959fbdc30 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 1 Sep 2020 08:58:14 -0700 Subject: [PATCH 18/64] capitalize first letter complete --- medium/capitalize-the-first-letter-of-each-word/code.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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; From 650e360020da421720f6dba66d7443254a9580fb Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 2 Sep 2020 09:55:28 -0700 Subject: [PATCH 19/64] capitalize name complete --- medium/capitalize-the-names/code.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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; From a0b6a3dd6e156f1450b63f57b51e469b95da569e Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 3 Sep 2020 10:09:07 -0700 Subject: [PATCH 20/64] anagram challenge complete --- medium/check-for-anagrams/code.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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; From fa91b79acfe2a9c350d65afdca895306aa77507c Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 4 Sep 2020 09:32:50 -0700 Subject: [PATCH 21/64] check prime number complete --- medium/check-if-a-number-is-prime/code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From f78a87a2bd45d0616b3beb1222be7b5706ac9100 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 5 Sep 2020 12:54:02 -0700 Subject: [PATCH 22/64] check array done --- medium/check-if-one-array-is-subset-of-another/code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From f4b5aa5e54ba1f89f7d82333cce813c3343afb99 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 6 Sep 2020 09:22:14 -0700 Subject: [PATCH 23/64] inital files for palindrome --- medium/check-if-the-string-is-a-palindrome/code.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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..28eee88d 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,6 @@ function isPalindrome(str) { - // Your code here. + let newStr = str.replace(" ", ""); + console.log(newStr, str) } module.exports = isPalindrome; From a6068a60fe67ac795df1d7189fdf2205370b39ed Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 7 Sep 2020 10:34:37 -0700 Subject: [PATCH 24/64] check palindrome complete --- medium/check-if-the-string-is-a-palindrome/code.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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 28eee88d..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,6 +1,12 @@ function isPalindrome(str) { - let newStr = str.replace(" ", ""); - console.log(newStr, str) + 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; From 8b650cdf01dad1541eaef8cbf4a6d20e17685123 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 8 Sep 2020 10:17:24 -0700 Subject: [PATCH 25/64] chocolate excercise complete --- medium/chocolate-dilemma/code.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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; From 4912be6322b4861971aa7b3d411340c392e163af Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 9 Sep 2020 13:51:10 -0700 Subject: [PATCH 26/64] attempted tree --- medium/christmas-tree/code.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/medium/christmas-tree/code.js b/medium/christmas-tree/code.js index 91af3656..819ce2a7 100644 --- a/medium/christmas-tree/code.js +++ b/medium/christmas-tree/code.js @@ -1,5 +1,28 @@ function tree(h) { - // Your code here. + const cTree = []; + if(h === 1) { + cTree.push("#") + return cTree; + } + for (let i = 1; i < h + 1; i++) { + let layer = ""; + for (let k = 1; k < h - i ; k++) { + layer += " "; + } + for (let j = 0; j < h; j++) { + if (j === 0) { + layer += "#"; + } else { + layer += "##" + } + } + for (let k = 1; k < h - i; k++) { + layer += " "; + } + console.log(cTree) + cTree.push(layer); + } + return cTree; } module.exports = tree; From a0919d7f19e19ce62143155d3cd1f6f99e51ad02 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 10 Sep 2020 08:09:43 -0700 Subject: [PATCH 27/64] attempted tree --- medium/christmas-tree/code.js | 1 - 1 file changed, 1 deletion(-) diff --git a/medium/christmas-tree/code.js b/medium/christmas-tree/code.js index 819ce2a7..5680b810 100644 --- a/medium/christmas-tree/code.js +++ b/medium/christmas-tree/code.js @@ -19,7 +19,6 @@ function tree(h) { for (let k = 1; k < h - i; k++) { layer += " "; } - console.log(cTree) cTree.push(layer); } return cTree; From 4cbcd4198d516a4b3c15a5552f227d72c4de926e Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 11 Sep 2020 08:10:38 -0700 Subject: [PATCH 28/64] attempted tree --- medium/christmas-tree/code.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/medium/christmas-tree/code.js b/medium/christmas-tree/code.js index 5680b810..206a3ec9 100644 --- a/medium/christmas-tree/code.js +++ b/medium/christmas-tree/code.js @@ -1,22 +1,18 @@ function tree(h) { const cTree = []; - if(h === 1) { - cTree.push("#") - return cTree; - } for (let i = 1; i < h + 1; i++) { let layer = ""; - for (let k = 1; k < h - i ; k++) { + for (let k = 0; k < h - i ; k++) { layer += " "; } - for (let j = 0; j < h; j++) { + for (let j = 0; j < i; j++) { if (j === 0) { layer += "#"; } else { layer += "##" } } - for (let k = 1; k < h - i; k++) { + for (let k = 0; k < h - i; k++) { layer += " "; } cTree.push(layer); From e720436f710abde25a666d228dd5a470b19fb166 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 12 Sep 2020 10:04:51 -0700 Subject: [PATCH 29/64] Compoundiing letters --- medium/compounding-letters/code.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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; From d39f6e1f7dc68e54fa23df93765ebcd80a24db76 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 13 Sep 2020 00:49:04 -0700 Subject: [PATCH 30/64] convenience complete --- medium/convenience-store/code.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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; From 5b85ad3ca9d8a520ea0b909e44bf8a626fd041b6 Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 14 Sep 2020 01:11:58 -0700 Subject: [PATCH 31/64] convert key values to array --- medium/convert-key-values-in-an-object-to-array/code.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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; From baa898b5ca34b0c2941958b5c8ef3b547ea03d40 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 15 Sep 2020 03:28:27 -0700 Subject: [PATCH 32/64] convert number complete --- .../code.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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; From 82f8c90ef7ddbcb0ee1039357a8d2a2bc62b9d88 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 16 Sep 2020 02:08:51 -0700 Subject: [PATCH 33/64] count letter --- medium/count-letters-in-a-word-search/code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From a9977e1fbc5ac9f92d0fba67450d2fdd973de7f4 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 17 Sep 2020 00:42:32 -0700 Subject: [PATCH 34/64] count one binary --- .../code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From 3d605f162840dce90e888a0b79262cdec1d63221 Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 18 Sep 2020 02:14:54 -0700 Subject: [PATCH 35/64] count letters and digits --- medium/count-the-letters-and-digits/code.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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; From 4066e76f5d05f28950a0175a50804a97a8b13ac9 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 19 Sep 2020 03:30:09 -0700 Subject: [PATCH 36/64] count dup complete --- .../code.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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; From d7654349cc17953e5e68ef89bf58aada1b3262f3 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 1 Nov 2020 01:57:09 -0800 Subject: [PATCH 37/64] count char in string --- easy/count-instances-of-a-character-in-a-string/code.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; From eda86f536e1d27b21b1e657d4c00e3283885058d Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 2 Nov 2020 09:16:48 -0800 Subject: [PATCH 38/64] counted ones complete --- easy/count-ones-in-a-2d-array/code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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; From 0eafbe0387b1c03987588b9e879a76421861edee Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 3 Nov 2020 09:00:14 -0800 Subject: [PATCH 39/64] even or odd complete --- easy/even-or-odd-number-of-factors/code.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; From 2dddfa559ef72404a632f8ebb6e5026d7f8a7d17 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 4 Nov 2020 07:16:52 -0800 Subject: [PATCH 40/64] factor chain complete --- easy/factor-chain/code.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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; From a64ec7a4102ff8e2649ee7b676ca7d2d6fc58573 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 5 Nov 2020 00:20:49 -0800 Subject: [PATCH 41/64] filter out string complete --- easy/filter-out-strings-from-an-array/code.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; From 5ee02f9159c366c7640a54841400a4cbd2f6fa51 Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 6 Nov 2020 00:22:07 -0800 Subject: [PATCH 42/64] find largest complete --- .../code.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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; From 9c2dca93bfb836ff5049076575ee9de36a8a7a1e Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 7 Nov 2020 07:36:16 -0800 Subject: [PATCH 43/64] find the min max average --- .../code.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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; From ce36426a37342d1faa9ae4e5f315a49521d42484 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 8 Nov 2020 00:10:52 -0800 Subject: [PATCH 44/64] find min and max complete --- .../code.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) 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; From 9856dd6d7bc84b2c1fa3b263bc190be20d9e586c Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 18 Mar 2021 00:06:26 -0700 Subject: [PATCH 45/64] finding repeat --- hard/find-repeating/code.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hard/find-repeating/code.js b/hard/find-repeating/code.js index a5eea26a..7a08305a 100644 --- a/hard/find-repeating/code.js +++ b/hard/find-repeating/code.js @@ -1,5 +1,13 @@ 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]); + } + } } module.exports = findRepeating; From 7545e3305f65a37f6e25dce0e283a48acf5f59f1 Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 19 Mar 2021 00:00:36 -0700 Subject: [PATCH 46/64] finding repeat --- hard/find-repeating/code.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hard/find-repeating/code.js b/hard/find-repeating/code.js index 7a08305a..48837c41 100644 --- a/hard/find-repeating/code.js +++ b/hard/find-repeating/code.js @@ -8,6 +8,8 @@ function findRepeating(str) { tempArr.push(str[i]); } } + + return arr; } module.exports = findRepeating; From 4445dbcda12d825ef6f132b9a1a2758c5d246c35 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 20 Mar 2021 00:08:16 -0700 Subject: [PATCH 47/64] shared letter --- hard/find-the-shared-letters-between-two-strings/code.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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..be9124be 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,12 @@ function sharedLetters(str1, str2) { - // Your code here. + const copies = []; + for (let i = 0; i < str1.length; i++) { + if (str2.includes(str1[i])) { + copies.push(str1[i]); + } + } + + return copies; } module.exports = sharedLetters; From c3ec00665b7d4eb5a6be80543b6dd8fd264571da Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 21 Mar 2021 08:50:57 -0700 Subject: [PATCH 48/64] shared letter --- hard/find-the-shared-letters-between-two-strings/code.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 be9124be..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,12 +1,16 @@ function sharedLetters(str1, str2) { const copies = []; + str1 = str1.toLowerCase(); + str2 = str2.toLowerCase(); for (let i = 0; i < str1.length; i++) { - if (str2.includes(str1[i])) { + if (str2.includes(str1[i]) && !copies.includes(str1[i])) { copies.push(str1[i]); } } - return copies; + copies.sort(); + + return copies.join(""); } module.exports = sharedLetters; From 363b73afef94990b39f68a579e1ac8874a2b473d Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 22 Mar 2021 00:35:13 -0700 Subject: [PATCH 49/64] find the shortest --- .../code.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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..3408941f 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) { + shortest.push(words[i].toLowerCase()); + } + } + + console.log(words, wordLength, min); + return shortest.sort(); } module.exports = findShortestWords; From 70a7794f5684b40d99eba04478fc2322e7b4fb2b Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 23 Mar 2021 07:30:29 -0700 Subject: [PATCH 50/64] find the shortest --- hard/find-the-shortest-word-s-in-a-sentence/code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3408941f..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 @@ -9,7 +9,7 @@ function findShortestWords(str) { } for (let i = 0; i < words.length; i++) { - if (words[i].length === min) { + if (words[i].length === min && isNaN(parseInt(words[i]))) { shortest.push(words[i].toLowerCase()); } } From ea37aab1d6da52927e3790a9e994e928b9b78223 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 24 Mar 2021 00:17:01 -0700 Subject: [PATCH 51/64] flip array --- hard/flip-the-array/code.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index 08dd005f..badab8a3 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -1,5 +1,7 @@ function flipArray(arr) { - // Your code here. + let columns = arr[0].length; + + console.log(arr[0], columns) } module.exports = flipArray; From 52f06da22e11ce2bf8d3069927dc7df94f918ef9 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 25 Mar 2021 00:31:14 -0700 Subject: [PATCH 52/64] flip array --- hard/flip-the-array/code.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index badab8a3..2f9e5569 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -1,6 +1,15 @@ function flipArray(arr) { + const newArr = []; let columns = arr[0].length; + + if (columns === undefined) { + for (let i = 0; i < arr.length; i++) { + newArr.push([arr[i]]); + } + } + + console.log(arr[0], columns) } From d829506c545e86de5219e48b34278221064be909 Mon Sep 17 00:00:00 2001 From: John Ko Date: Fri, 26 Mar 2021 00:52:25 -0700 Subject: [PATCH 53/64] flip array --- hard/flip-the-array/code.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index 2f9e5569..628f3a50 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -9,8 +9,9 @@ function flipArray(arr) { } } - console.log(arr[0], columns) + + return newArr; } module.exports = flipArray; From 4c1b317918e30cdd0d556f0bd5fccd860fddc57c Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 27 Mar 2021 00:55:00 -0700 Subject: [PATCH 54/64] flip array --- hard/flip-the-array/code.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index 628f3a50..d5cbd139 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -7,6 +7,10 @@ function flipArray(arr) { 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]); + } } console.log(arr[0], columns) From 3c31cfbe17b2505f949168b5ce4bb791a8774842 Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 28 Mar 2021 00:54:02 -0700 Subject: [PATCH 55/64] flip array --- hard/flip-the-array/code.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index d5cbd139..8ac7d87f 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -1,15 +1,17 @@ function flipArray(arr) { + 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]); + newArr.push(arr[i][0]); } } From fac6a97a024019843caccc878022fb7e0af0b78c Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 29 Mar 2021 00:00:27 -0700 Subject: [PATCH 56/64] flip array --- hard/flip-the-array/code.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/hard/flip-the-array/code.js b/hard/flip-the-array/code.js index 8ac7d87f..290a128b 100644 --- a/hard/flip-the-array/code.js +++ b/hard/flip-the-array/code.js @@ -15,8 +15,6 @@ function flipArray(arr) { } } - console.log(arr[0], columns) - return newArr; } From 38ab2b2c601361de406591be07fc772674cbd4fa Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 29 Mar 2021 23:58:53 -0700 Subject: [PATCH 57/64] fridge poetry --- hard/fridge-poetry/code.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hard/fridge-poetry/code.js b/hard/fridge-poetry/code.js index d75ccc1c..0619bc00 100644 --- a/hard/fridge-poetry/code.js +++ b/hard/fridge-poetry/code.js @@ -1,5 +1,15 @@ 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; + } + } + + console.log(string1, string2); + return true; } module.exports = canBuild; From c9d4fc08a47fbafcee54b3b5b1a08cd724732f51 Mon Sep 17 00:00:00 2001 From: John Ko Date: Wed, 31 Mar 2021 00:33:46 -0700 Subject: [PATCH 58/64] fridge poetry --- hard/fridge-poetry/code.js | 1 - 1 file changed, 1 deletion(-) diff --git a/hard/fridge-poetry/code.js b/hard/fridge-poetry/code.js index 0619bc00..772866bc 100644 --- a/hard/fridge-poetry/code.js +++ b/hard/fridge-poetry/code.js @@ -8,7 +8,6 @@ function canBuild(s1, s2) { } } - console.log(string1, string2); return true; } From d5672ae5dbaa7662180c177f10efff60af0991b1 Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 1 Apr 2021 00:03:59 -0700 Subject: [PATCH 59/64] game of throne name --- hard/game-of-thrones-character-titles/code.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hard/game-of-thrones-character-titles/code.js b/hard/game-of-thrones-character-titles/code.js index 1ed423ef..bb24fe97 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; From ffdecb39b5f26ea952be679ccc32bb7df2f2bc4f Mon Sep 17 00:00:00 2001 From: John Ko Date: Thu, 1 Apr 2021 22:25:06 -0700 Subject: [PATCH 60/64] game of throne name --- hard/game-of-thrones-character-titles/code.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hard/game-of-thrones-character-titles/code.js b/hard/game-of-thrones-character-titles/code.js index bb24fe97..39ded621 100644 --- a/hard/game-of-thrones-character-titles/code.js +++ b/hard/game-of-thrones-character-titles/code.js @@ -1,7 +1,7 @@ function correctTitle(str) { const words = str.split(" ") for (let i = 0; i < words.length; i++) { - const currentWord = words[i].toLowerCase(); + const currentWord = words[i].toLowerCase(); if (currentWord === "and" || currentWord === "the" || currentWord === "of" || currentWord === "in") { words[i] = currentWord; From 7e6d0f366041484ea8ef32e68b11bf44f4dfde9d Mon Sep 17 00:00:00 2001 From: John Ko Date: Sat, 3 Apr 2021 01:14:02 -0700 Subject: [PATCH 61/64] gold distribution --- hard/gold-distribution/code.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/hard/gold-distribution/code.js b/hard/gold-distribution/code.js index 9679af60..86ca32ec 100644 --- a/hard/gold-distribution/code.js +++ b/hard/gold-distribution/code.js @@ -1,5 +1,18 @@ 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++) { + console.log(tolerance[i], (max - gold[i])) + if (tolerance[i] <= (max - gold[i])) { + return true; + } + } + console.log(max); + return false; } module.exports = piratesKilled; From 8c030ffb90cdd3c36af4270a1c0216d5470be0ab Mon Sep 17 00:00:00 2001 From: John Ko Date: Sun, 4 Apr 2021 00:15:10 -0700 Subject: [PATCH 62/64] gold distribution --- hard/gold-distribution/code.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hard/gold-distribution/code.js b/hard/gold-distribution/code.js index 86ca32ec..74477ad7 100644 --- a/hard/gold-distribution/code.js +++ b/hard/gold-distribution/code.js @@ -6,12 +6,10 @@ function piratesKilled(gold, tolerance) { } } for (let i = 0; i < gold.length; i++) { - console.log(tolerance[i], (max - gold[i])) - if (tolerance[i] <= (max - gold[i])) { + if (tolerance[i] < (max - gold[i])) { return true; } } - console.log(max); return false; } From c35d128f58ffa29d28fa2bf8f922380b575d6cd5 Mon Sep 17 00:00:00 2001 From: John Ko Date: Mon, 5 Apr 2021 08:31:53 -0700 Subject: [PATCH 63/64] grocery --- hard/grocery-store-prices/code.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hard/grocery-store-prices/code.js b/hard/grocery-store-prices/code.js index 22079b39..c7f3846b 100644 --- a/hard/grocery-store-prices/code.js +++ b/hard/grocery-store-prices/code.js @@ -1,5 +1,10 @@ function getPrices(arr) { - // Your code here. + const prices = []; + for (let i = 0; i < arr.length; i++) { + prices.push(arr[i].match(/\(([^)]+)\)/)[1]) + } + + return prices; } module.exports = getPrices; From 107761b0dc82047f0437207fd8cce9ed783d6d03 Mon Sep 17 00:00:00 2001 From: John Ko Date: Tue, 6 Apr 2021 10:38:57 -0700 Subject: [PATCH 64/64] grocery --- hard/grocery-store-prices/code.js | 1 + 1 file changed, 1 insertion(+) diff --git a/hard/grocery-store-prices/code.js b/hard/grocery-store-prices/code.js index c7f3846b..35acae55 100644 --- a/hard/grocery-store-prices/code.js +++ b/hard/grocery-store-prices/code.js @@ -2,6 +2,7 @@ function getPrices(arr) { 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;