Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created March 16, 2023 01:42
Show Gist options
  • Save natafaye/d66715aeb0b275f72e2987cb8adfc69d to your computer and use it in GitHub Desktop.
Save natafaye/d66715aeb0b275f72e2987cb8adfc69d to your computer and use it in GitHub Desktop.
// https://www.codewars.com/kata/52fba66badcd10859f00097e/train/javascript
const VOWELS = "aeiouAEIOU"
function isNotVowel(letter) {
//const lowerLetter = letter.toLowerCase()
return !VOWELS.includes(letter)
// return lowerLetter !== "a" && lowerLetter !== "e"
// && lowerLetter !== "i" && lowerLetter !== "o"
// && lowerLetter !== "u"
}
function disemvowel(str) {
let result = ""
for(let i = 0; i < str.length; i++) {
const letter = str[i]
if(isNotVowel(letter)) {
result += letter // result = result + letter
}
}
//let result = str.split("").filter(letter => !VOWELS.includes(letter)).join("")
return result;
}
// for loop = know how many times you're going to loop when the loop starts
// while loop = just know when to stop, but you don't know when that will be
// https://www.codewars.com/kata/61c78b57ee4be50035d28d42/train/javascript
function mergeStrings(first, second){
const lastOfFirst = first[first.length - 1] // "e"
let foundIndex = second.length - 1 // start at the last index
while(foundIndex >= 0) { // we haven't gone off the end
if(second[foundIndex] === lastOfFirst) { // index2 = 2
let checkIndex1 = first.length - 2
let checkIndex2 = foundIndex - 1
let matches = true
while(checkIndex2 >= 0) {
if(first[checkIndex1] !== second[checkIndex2]) {
// There is no overlap
matches = false
break;
}
checkIndex1--
checkIndex2--
}
// If we found the overlap, return the answer without the overlap in the middle
if(matches) {
return first + second.slice(foundIndex + 1)
}
}
foundIndex--
}
// if you don't find that letter, then just concatenate
return first + second
}
// As a human being
"abaabaab"
"aabaabab"
// look at last letter of first string
// starting at the beginning of the second string, find that letter
// start going backwards in each string to see if they match
// if they match, take out that chunk of first string
// if you don't find that letter, then just concatenate
"abaabaabab"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment