Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!