-
-
Save Woodsphreaker/d4f70c4c41d34cbc04629292a5bd1bda 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 stringToArray = string => string.split(""); | |
const ALPHABET = [...Array(26).keys()].map(pre => String.fromCharCode(97 + pre)); | |
const findCharIndex = letter => ALPHABET.indexOf(letter); | |
const verifyLastCharIndex = char => findCharIndex(char) + 1 > ALPHABET.length - 1 | |
? 0 | |
: findCharIndex(char) + 1; | |
const toString = list => list.join(""); | |
const getChar = char => findCharIndex(char) >= 0 | |
? ALPHABET[verifyLastCharIndex(char)] | |
: char; | |
const resolve = string => { | |
const newStringArray = stringToArray(string) | |
.map(getChar); | |
const newString = toString(newStringArray); | |
const upperVowels = newString.replace(/[aeiou]/g, (char) => char.toUpperCase()); | |
const result = upperVowels; | |
return result; | |
}; | |
console.log(resolve("hello*3")); //Ifmmp*3 | |
console.log(resolve("fun times!")); //gvO Ujnft! |
alphabetBuilder
is a value, not a function anymore.
I recommend you to make a const named ALPHABET
const ALPHABET = [ ...Array(26).keys() ].map( pre => String.fromCharCode( 97 + pre ) );
const stringToArray = string => string.split("");
const findCharIndex = letter => ALPHABET.indexOf(letter);
const toString = list => list.join("");
const mapTo = arr => arr.map( toAlphabet( ALPHABET ) );
const verifyLastCharIndex = char =>
findCharIndex(char) + 1 > ALPHABET.length - 1
? 0
: findCharIndex(char) + 1;
const toAlphabet = ALPHABET => char =>
( findCharIndex( char ) >= 0 )
? ALPHABET[verifyLastCharIndex(char)]
: char
const transformToUpperCase = newString =>
newString.replace( /[aeiou]/g, char => char.toUpperCase() );
const result = string =>
transformToUpperCase( toString( mapTo( stringToArray( string ) ) ) );
console.log(result("hello*3"));
console.log(result("fun times!"));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
caraca bem legal sua solução. com alphabetBuilder curti mesmo