Last active
July 19, 2019 08:51
-
-
Save kyranjamie/ff1f590210cb877bd6925760310e5ce8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getNextIndex = (currentIndex: number, array: any[]) => | |
currentIndex === array.length - 1 ? 0 : currentIndex + 1; | |
const getPreviousIndex = (currentIndex: number, array: any[]) => | |
currentIndex === 0 ? array.length - 1 : currentIndex - 1; | |
function moveLetters (input: string) { | |
const sentence = input.split(' '); | |
return sentence | |
.map((currentWord, index) => { | |
const nextWord = sentence[getNextIndex(index, sentence)]; | |
const previousWord = sentence[getPreviousIndex(index, sentence)]; | |
const strippedWord = currentWord.substr(1, currentWord.length - 2); | |
return nextWord[0] + strippedWord + previousWord[previousWord.length - 1]; | |
}); | |
} | |
console.log(moveLetters('bes le uoogit')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment