Skip to content

Instantly share code, notes, and snippets.

@hafidbuilds
Last active December 18, 2018 08:27
Show Gist options
  • Save hafidbuilds/4b563ecf0b4f0af157fc24c55cae1b82 to your computer and use it in GitHub Desktop.
Save hafidbuilds/4b563ecf0b4f0af157fc24c55cae1b82 to your computer and use it in GitHub Desktop.
function toPigLatin(strings) {
const words = strings.split(' ')
const startAt = ''
function swapAndTranslate(accumulator, currentAlphabet) {
const translator = ['a', 'y']
let swapCharacter = [...currentAlphabet.toLowerCase()]
const firstAlphabet = swapCharacter[0]
swapCharacter[swapCharacter.length] = firstAlphabet
swapCharacter = [
...swapCharacter.slice(1),
...translator
].join('')
const results = accumulator + swapCharacter
return results.charAt(0).toUpperCase() + results.slice(1) + ' '
}
return words
.reduce(swapAndTranslate, startAt)
.trim()
}
toPigLatin('The quick brown fox')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment