Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
How to create an object function that randomly change a element in the object's property

I am trying to create a factory function that comes with a mutate() method that takes a random element in the object.dna property and change it to a random base from bases = ['A', 'T', 'C', 'G']. However, the element randomly selected from object.dna has to be changed to an different base, and it cannot remain the same.

const pAequorFactory = (specimenNum, dna) => {
  return {
  specimenNum,
  dna,
  mutate() {
      const bases = ['A', 'T', 'C', 'G'];
      let selectedBase = this.dna[Math.floor(Math.random()*this.dna.length)];
      let randomBase
              do {
              randomBase =  bases[Math.floor(Math.random()*bases.length)];
              // console.log(randomBase);
          } while (selectedBase === randomBase);}
                 }                  
              }


const a = [
  'G', 'C', 'A', 'T',
  'C', 'G', 'G', 'A',
  'C', 'G', 'G', 'T',
  'T', 'A', 'G'
]
let b =pAequorFactory(17, a)
b.mutate;
console.log(b.dna);

I ran the code above and expected a random element in b.dna to change, but it didn't.

  • I used do... while so the randomly selected base would overwrite the selectedBase if different, and if not would randomly select another base.
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda