Created
November 25, 2023 04:59
-
-
Save kenneropia/aed1b37f6271b1b4ab0e3271fd761518 to your computer and use it in GitHub Desktop.
mergeWord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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