Skip to content

Instantly share code, notes, and snippets.

@j10sanders
Created June 22, 2020 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j10sanders/a05ab651cedd43deb3cbd41295072add to your computer and use it in GitHub Desktop.
Save j10sanders/a05ab651cedd43deb3cbd41295072add to your computer and use it in GitHub Desktop.
const cycle = (input) => {
const arrInput = input.split('');
const firstLetters = [];
const lastLetters = [];
for (let i = 0; i < arrInput.length; i += 1) {
if (arrInput[i - 1] === ' ' || i === 0) {
firstLetters.push(arrInput[i]);
}
}
// return firstLetters;
for (let i = arrInput.length - 1; i > 0; i -= 1) {
if (arrInput[i + 1] === ' ' || i === arrInput.length - 1) {
lastLetters.push(arrInput[i]);
}
}
lastLetters.reverse();
let currentLetterIndex = 1;
let currentLastLetter = 1;
for (let i = 0; i > 0; i -= 1) {
if (arrInput[i + 1] === ' ') {
arrInput[i] = lastLetters[currentLastLetter];
currentLastLetter += 1;
}
}
for (let i = 0; i < arrInput.length; i += 1) {
if (arrInput[i - 1] === ' ' || i === 0) {
if (currentLetterIndex === firstLetters.length) {
arrInput[i] = firstLetters[0];
} else {
arrInput[i] = firstLetters[currentLetterIndex];
currentLetterIndex += 1;
}
}
}
return arrInput.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment