Skip to content

Instantly share code, notes, and snippets.

@Matheus-de-Souza
Forked from lubien/excercicio.txt
Created March 27, 2017 18:34
Show Gist options
  • Save Matheus-de-Souza/3180e06c5005911849b7784f11e0e8fe to your computer and use it in GitHub Desktop.
Save Matheus-de-Souza/3180e06c5005911849b7784f11e0e8fe to your computer and use it in GitHub Desktop.
Exercicio - transformar string
Transformar uma string em outra na qual cada letra do alfabeto deve ser a proxima mantendo o resto igual. ex: a -> b, z -> a, f -> g.
Após a transformação gerar uma nova string onde toda vogal deve ser maiúscula.
exemplos:
Input:"hello*3"
Output:"Ifmmp*3"
Input:"fun times!"
Output:"gvO Ujnft!"
const
pipe = (...fs) => y => fs.reduce((x, f) => f(x), y)
, LETTER_A = 97
, LETTER_Z = 122
, codeIsLetter = code => code >= LETTER_A && code <= LETTER_Z
, nextLetterCode = code => code == LETTER_Z ? LETTER_A : code + 1
, vowel = char => ['a', 'e', 'i', 'o', 'u'].some(c => c === char)
, solve = str =>
str
.split('')
.map(pipe(
char => char.charCodeAt(0)
, code => codeIsLetter(code) ? nextLetterCode(code) : code
, code => String.fromCodePoint(code)
, char => vowel(char) ? char.toUpperCase() : char
))
.join('')
console.log(solve('hello*3'), solve('hello*3') === 'Ifmmp*3')
console.log(solve('fun times!'), solve('fun times!') === 'gvO Ujnft!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment