Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Forked from rafaelassumpcao/excercicio.txt
Last active March 29, 2017 15:14
Show Gist options
  • Save Woodsphreaker/d4f70c4c41d34cbc04629292a5bd1bda to your computer and use it in GitHub Desktop.
Save Woodsphreaker/d4f70c4c41d34cbc04629292a5bd1bda 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 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!
@rafaelassumpcao
Copy link

caraca bem legal sua solução. com alphabetBuilder curti mesmo

@lubien
Copy link

lubien commented Mar 23, 2017

alphabetBuilder is a value, not a function anymore.

I recommend you to make a const named ALPHABET

@suissa
Copy link

suissa commented Mar 29, 2017

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