Last active
December 18, 2018 08:27
-
-
Save hafidbuilds/4b563ecf0b4f0af157fc24c55cae1b82 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
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