Skip to content

Instantly share code, notes, and snippets.

@rafaelassumpcao
Last active March 23, 2017 18:51
Show Gist options
  • Save rafaelassumpcao/7f1170400ba487af79efd041b928c805 to your computer and use it in GitHub Desktop.
Save rafaelassumpcao/7f1170400ba487af79efd041b928c805 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 str = "hello*3d";
const stringToArray = (str, strLength = 0, arr = []) => {
if(strLength === str.length) return arr;
arr.push(str[strLength])
return stringToArray(str, strLength + 1, arr );
};
const isLetter = char => char.match(/\w/) !== null;
const isLetterZ = char => char === "z";
const isVowel = char => {
let t = char.match(/[aeiou]/)
return t ? t[0].toUpperCase() : char;
};
const vowelToUpper = fn => char => fn(char);
const changeLetter = char => {
return isLetterZ(char) ? "a" :
isLetter(char) ?
String.fromCharCode(char.charCodeAt(0) + 1) : char;
};
const map = fn => xs => xs.map(fn);
const join = (xs, separator = "") => xs.join(separator);
const derivedWord = map(changeLetter)(stringToArray(str));
const newWord = map(vowelToUpper(isVowel))(derivedWord)
const finalWord = join(newWord);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment