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

214
Visualizações
Extra elements being added to an array

I was attempting to complete this problem as a newbie, and I'm seeing extra elements within my returned array after my loop.

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". 
You have function with one side of the DNA, you need to get the other complementary side.

Here is my solution

let dnaComStrand = [];
const DNAStrand = function(dnaStrand) {
  for (let i = 0; i < dnaStrand.length; i++) {
    if (dnaStrand[i] === `A`) {
      dnaComStrand[i] = `T`;
    } else if (dnaStrand[i] === `T`) {
      dnaComStrand[i] = `A`;
    } else if (dnaStrand[i] === `C`) {
      dnaComStrand[i] = `G`;
    } else if (dnaStrand[i] === `G`) {
      dnaComStrand[i] = `C`;
    }
  }
  //   return dnaComStrand.join(``);
  return dnaComStrand;
};
console.log(DNAStrand(`GCATA`));

yet in the console I was seeing

Array(5) [ "C", "G", "T", "A", "T" ]

which became even more confusing if I tried to call the function again.

let dnaComStrand = [];
const DNAStrand = function(dnaStrand) {
  for (let i = 0; i < dnaStrand.length; i++) {
    if (dnaStrand[i] === `A`) {
      dnaComStrand[i] = `T`;
    } else if (dnaStrand[i] === `T`) {
      dnaComStrand[i] = `A`;
    } else if (dnaStrand[i] === `C`) {
      dnaComStrand[i] = `G`;
    } else if (dnaStrand[i] === `G`) {
      dnaComStrand[i] = `C`;
    }
  }
  //   return dnaComStrand.join(``);
  return dnaComStrand;
};
console.log(DNAStrand(`GCATA`));
console.log(DNAStrand([`T`, `C`, `G`]));

which resulted in

Array(5) [ "A", "G", "C", "A", "T" ]

Where are these extra elements coming from?

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

0

You can do:

const hash = {
  A: 'T',
  T: 'A',
  C: 'G',
  G: 'C',
}

const DNAStrand = dnaStrand => dnaStrand.split('').map(c => hash[c])

// 1
const dnaComStrand1 = DNAStrand(`GCATA`)
console.log(dnaComStrand1)

// 2
const dnaComStrand2 = DNAStrand(dnaComStrand1.join(''))
console.log(dnaComStrand2)

about 4 years ago · Juan Pablo Isaza Relatório

0

Reinitialize the array with new set of elements inside the function and before the loop.

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