Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
Encrypt using regex

I had a question I was able to solve for encrypting using regex and I like the solution just wondering if there is a way I can clean it up a bit? for the encryption im supposed to replace the vowels with these values and reverse the string adding aca to the end. just wondering if I can reduce the amount .replace lines thanks in advance

function encrypt(string) {
  let input=string.replace(/a/g, '0')
                  .replace(/e/g, '1')
                  .replace(/i/g, '2')
                  .replace(/o/g, '2')
                  .replace(/u/g, '3')
  return input.split("").reverse().join("")+"aca"
              
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You might use a single replace with a callback function, and in the function map the values to the numbers.

function encrypt(string) {
    const obj = {'a': 0, 'e': 1, 'i': 2, 'o': 2, 'u': 3};
    return string
        .replace(/[aeiou]/g, m => obj[m])
        .split("")
        .reverse()
        .join("")+"aca";
}

console.log(encrypt('this is an example, our example'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Known characters one to one replacement, regexp is not required.

const encrypt = (str) => {
  const map = { a: '0', e: '1', i: '2', o: '2', u: '3' };
  return str.split('').map(ch => map[ch] || ch).reverse().join('') + 'aca';
}

console.log(encrypt('hello mario'));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda