Skip to content

Instantly share code, notes, and snippets.

@kyranjamie
Last active July 19, 2019 08:51
Show Gist options
  • Save kyranjamie/ff1f590210cb877bd6925760310e5ce8 to your computer and use it in GitHub Desktop.
Save kyranjamie/ff1f590210cb877bd6925760310e5ce8 to your computer and use it in GitHub Desktop.
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