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

69
Visualizações
Convert string letters to different ones in JS

I have this simple JS task that i can't figure out, not so simple for me after all.

Create a function 'converter', that converts letters in this way:

T -> C
C -> T
A -> D
X -> Y

Function accepts String with letters. All non convertable letters should be removed from string.

Example:

 - converter('TTCCAAXX') -> 'CCTTDDYY'
 - converter('TTGG') -> 'CC'

I tried something like this -

function Converter(str) {
let upperStr = str.toUpperCase()
let newStr = upperStr.replaceAll("T", "C").replaceAll("C", "T")
console.log(newStr)
}

Converter("TTCCAAXX")

But this is replacing all the characters and overwriting them and I'm not getting the right results.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use a sequential for loop which is more efficient (only requires one iteration) and solves the overwriting issue.

For better flexibility we can store the character pairs in an object.

const convertKeyValue = {
  'T': 'C',
  'C': 'T',
  'A': 'D',
  'X': 'Y'
}

function Converter(str) {
  let upperStr = str.toUpperCase()
  var newStr = ''
  for(let i = 0; i < upperStr.length; i++){
    const current = upperStr.charAt(i);
    newStr += convertKeyValue[current] ? convertKeyValue[current] : '';
  }
  return newStr;
}

console.log(Converter('TTCCAAXX'));
console.log(Converter('TTGG'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Consider adding a boolean/counter to represent the state in addition to new mappings. That way you don't get recurring or cyclical conversions.

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