Created
June 22, 2020 21:21
-
-
Save j10sanders/a05ab651cedd43deb3cbd41295072add 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 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