Click here to Skip to main content
15,908,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript


I'm trying to print an array of 15 elements to the console.
Currently it only prints something like ['G'] or ['A'].

I thought my splice code would work because splice works by basically saying:

(at this element number, replace this amount of elements, with this string)

so, see my splice formula below:

//this.DNA.splice(Math.floor(Math.random()*15), 1, returnRandBase());

Should surely translate to: (0 - 14, 1, A or T or C or G)

// Returns a random DNA base
  const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G']
  return dnaBases[Math.floor(Math.random() * 4)] 
}

// Returns a random single stand of DNA containing 15 bases
  const mockUpStrand = () => {
  const newStrand = []
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase())
  }
  return newStrand
}

//factory function 
  const pAequorFactory = (specimenNum, DNA) => {
  return {
    specimenNum: specimenNum,
    DNA: DNA,

    //mutate takes in a 15 element array of DNA letters, picks a random element, and 
      changes
    //it's letter to something else. Letters are 'A', 'T', 'C', 'G'. It must not 
    //change it to the same letter.

    mutate(){
    let result =[];
    result = this.DNA.splice(Math.floor(Math.random()*15), 1, returnRandBase());
    return result;
    }
  }
};

var newObj = pAequorFactory(15, ['A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C']);

console.log(newObj.mutate());


What I have tried:

Nothing as it looks right to me so maybe someone can spot something
Posted
Updated 11-Jan-22 12:40pm

1 solution

Return this.DNA from mutate() instead of 'result'. Check splice documentation for more details... 'splice is a mutable function because it modifies the original array, while slice returns a new array'


// Returns a random DNA base
  const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G']
  return dnaBases[Math.floor(Math.random() * 4)] 
}

// Returns a random single stand of DNA containing 15 bases
  const mockUpStrand = () => {
  const newStrand = []
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase())
  }
  return newStrand
}

//factory function 
  const pAequorFactory = (specimenNum, DNA) => {
  return {
    specimenNum: specimenNum,
    DNA: DNA,

    //mutate takes in a 15 element array of DNA letters, picks a random element, and changes
    //it's letter to something else. Letters are 'A', 'T', 'C', 'G'. It must not 
    //change it to the same letter.

    mutate(){
    let result =[];
    this.DNA.splice(Math.floor(Math.random()*15), 1, returnRandBase());
    return this.DNA;
    }
  }
};

var newObj = pAequorFactory(15, ['A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C']);

console.log(newObj.mutate());
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900