Last active
March 23, 2017 18:51
-
-
Save rafaelassumpcao/7f1170400ba487af79efd041b928c805 to your computer and use it in GitHub Desktop.
Exercicio - transformar string
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
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!" |
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 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
https://gist.github.com/lubien/07af7b43231d7e584f4a23b0e1e175a4