Skip to content

Instantly share code, notes, and snippets.

@kyranjamie
Last active Jul 19, 2019
Embed
What would you like to do?
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