Skip to content

Instantly share code, notes, and snippets.

@kenneropia
Created November 25, 2023 04:59
Show Gist options
  • Save kenneropia/aed1b37f6271b1b4ab0e3271fd761518 to your computer and use it in GitHub Desktop.
Save kenneropia/aed1b37f6271b1b4ab0e3271fd761518 to your computer and use it in GitHub Desktop.
mergeWord
const mergeWord = (word1: string, word2: string) => {
let shorterWord = ''
let longerWord = ''
let accu = ''
if (word1.length < word2.length) {
shorterWord = word1
longerWord = word2
} else if (word2.length < word1.length) {
shorterWord = word2
longerWord = word1
} else {
shorterWord = word1
longerWord = word2
}
for (let i = 0; i < shorterWord.length; i++) {
const element = shorterWord[i];
accu += element
if (longerWord[i]) {
accu += longerWord[i]
}
}
accu += longerWord.substring(shorterWord.length, longerWord.length)
return accu
}
const result = mergeWord("abcDc", "pqrkja")
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment