Mysterious Organism Challenge Project (JavaScript)

Can someone tell me what’s wrong with my code? The compare() method seems not to be working.

// 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;
};

function pAequorFactory(specimenNum, dna) {
  return {
    specimenNum: specimenNum,
    dna: dna,
    mutate() {
      let randoDNA = returnRandBase();
      const newDNA = [];
      const Bases = ['A', 'T', 'C', 'G'];
      Bases.forEach(element => {
        if (element !== randoDNA) {
          newDNA.push(element);
        }
      });
      const replacingDNA = newDNA[Math.floor(Math.random() * 3)];
      this.dna[this.dna.indexOf(randoDNA)] = replacingDNA;
      return this.dna;

    },
    compareDNA(pAequor) {
      if (this.specimenNum === pAequor.specimenNum) {
        return 'Specimen number provided has already been assigned to another specimen';
      } else {
        const indexLocation1 = [];
        const indexLocation2 = [];
        const commonDNABases = [];
        for (let i = 0; i <= this.dna.length; i++) {
          for (let j = 0; j < pAequor[dna].length; j++) {
            if (this.dna[i] === pAequor[J]) {
              commonDNABases.push(pAequor[j]);
              indexLocation1.push(i);
              indexLocation2.push(j);
            }
          }
        }
        const numbersOfDNAInI = indexLocation1.length;
        const numbersOfDNAInJ = indexLocation2.length;
        const overallLengthOfFirstDNA = this.dna.length;
        const overallLengthOfSecondDNA = pAequor[dna].length;
        const pCompositionOfFirstDNA = (numbersOfDNAInI / overallLengthOfFirstDNA) * 100;
        const pCompositionOfSecondDNA = (numbersOfDNAInJ / overallLengthOfSecondDNA) * 100;

        if (pCompositionOfFirstDNA === pCompositionOfSecondDNA) {
          return `Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${pCompositionOfFirstDNA}%  DNA in common`;
        } else
          return `Specimen ${this.specimenNum} has ${pCompositionOfFirstDNA}% of its DNA in common with specimen ${pAequor.specimenNum} who has ${pCompositionOfSecondDNA}% of its DNA in common`;
      }
    },

    willLikelySurvive() {
      let countC = 0;
      let countG = 0;
      
      this.dna.forEach(d => {
        if (d === 'c') {
          countC++;
        } else if (d === 'G') {
          countG++;
        }
      });
      console.log(countC);
      const pCompositionOfC = (countC / this.dna.length) * 100;
      const pCompositionOfG = (countG / this.dna.length) * 100;
      if ((pCompositionOfC > 60) || (pCompositionOfG > 60)) {
        return true;
      } else
        return false;
    }

  }; //end of factory function that creates objects
} //end of function

const obj1 = pAequorFactory(1, ["T", "T", "A", "T", "G", "G", "A", "A", "A", "G", "T", "G", "T", "G", "A"]);
const obj2 = pAequorFactory(2,["C", "C", "C", "C", "C", "C", "C", "C", "C", "G", "T", "G", "T", "C", "C"])
console.log(obj2.willLikelySurvive());

Hi! This here is my code.

// 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 (make new specimen)
function pAequorFactory(number, array) {
  let object = {
    specimenNum: number, 
    dna: array,

    complementStrand() {
      let complementaryStrand = [];
      this.dna.forEach(function(base) {
        if (base === 'A') {
          complementaryStrand.push('T'); 
        } else if (base === 'T') {
          complementaryStrand.push('A'); 
        } else if (base === 'C') {
          complementaryStrand.push('G'); 
        } else if (base === 'G') {
          complementaryStrand.push('C'); 
        }
      })
      return complementaryStrand;
    },

    mutate() {
      let baseToMutate = Math.floor(Math.random() * 15);
      let originalBase = this.dna[baseToMutate];
      function mutation() {
        const dnaBases = ['A', 'T', 'C', 'G'];
        dnaBases.splice((dnaBases.indexOf(originalBase)), 1);
        return dnaBases[Math.floor(Math.random() * 3)];
      }
      let mutatedBase = mutation();
      this.dna[baseToMutate] = mutatedBase;
      return this.dna;
    },

    compareDNA(pAequor) {
      let counter = 0;
      for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === pAequor.dna[i]) {
          counter++;
        }
      }
      let percentage = (counter/this.dna.length) * 100;
      // console.log(`Specimen #${this.specimenNum} and Specimen #${pAequor.specimenNum} have ${percentage}% DNA in common.`);
      return percentage;
    },

    willLikelySurvive() {
      let counter = 0;
      this.dna.forEach(function(element) {
        if (element === 'C' || element === 'G') {
          counter++;
        }
      })
      let percentage = counter/this.dna.length * 100;
      // console.log(`Specimen #${this.specimenNum} has ${percentage}% of 'C' or 'G' in DNA. It's better likelyhood to survive is:`);
      return percentage >= 60;
    }    
    
  };
  return object;
};

//make a random population that is likely to survive
function makePAequorPopulation(populationNumber) {
  let array = [];
  for (i = 1; i < populationNumber+1; i++) {
    let individual = pAequorFactory(i, mockUpStrand());
    if (individual.willLikelySurvive() === true) {
      array.push(individual);
    } else {
      i--;
    }
  }
  return array;
}

//find two most related specimens
function mostRelated(array) {
  let strongestRelations = [];
  let mostRelated = 0;
  for (let i = 0; i < array.length; i++) {
    for (let j = i + 1; j < array.length; j++) {
      let comparison = array[i].compareDNA(array[j]);
      if (comparison > mostRelated) {
        mostRelated = comparison;
        strongestRelations = [(array[i].specimenNum), (array[j].specimenNum)];
      } else if (comparison === mostRelated) {
        strongestRelations.push((array[i].specimenNum), (array[j].specimenNum));
      }
    }
  }
  console.log(`The most related specimens have ${mostRelated}% DNA in common`);
  console.log('These are the most related specimens:')
  return strongestRelations;
}


let population = makePAequorPopulation(30);
// console.log(population);
console.log(population[1].dna) 
console.log(population[1].complementStrand());
console.log(mostRelated(population));

It was definitely challenging!
And it’s probably not the most concise and elegant manner, but it worked.
If someone has any suggestions how to improve it, I would really appreciate the help.
Thanks!

Ive had a rough start with objects but Im working hard!! When I log test the factory function by running a number for specimen and an array generated or made it does not list the dna values but does the specimen. Maybe I should revisit factory functions again…but can someone look over my code and possibly see whats wrong and give feedback. I know Im not understanding some basic rule.

// 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 = () => {
let newStrand = ;
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
};

const pAequorFactory = (number,array) => {
return{
specimenNum: number,
dna: array,

get dna() {
return this.dna
},
set dna(array) {
this.dna = array;
},
mutate() {
let originalBase = this.dna[Math.floor(Math.random() * 15)];
let newBase = ;
returnRandBase();
if(dnaBases != originalBase) {
newBase.push(dnaBases);
this.dna.splice(originalBase.findIndex(),1,newBase)
}
else {
while(dnaBases === originalBase){
returnRandBase();
newBase.push(dnaBases);
this.dna.splice(originalBase.findIndex(),1,newBase)
}
}
return this.dna
},
compareDNA(object) {
let commonPercent;
let commonValues = ;
for(i = 0; i < this.dna.length; i++) {
for(j = 0; j < object.length; i++) {
if(this.dna[i] === object[j]) {
commonValues.push(this.dna[i])
}
}
}
return commonValues;
commonPercent = (commonValues.length / 15) * 100;
return commonPercent;
console.log(specimen #1 and specimen #2 have${commonPercent}% DNA in common)
},
willLikelySurvive(object) {
let cAndG = ;
for(i = 0; i < object.dna.length; i++) {
if(object.dna[i] === ‘C’ || object.dna[i] === ‘G’){
cAndG.push(i);
}
}
return cAndG;
if(cAndG.length >= 9) {
return true
}
else {
return false
}
}
}
};

my solution! what a fun project. It definitely helped me get a better understanding of Objects


Here is my code.
Feel free to suggest any changes.
Thanx in advance.

Here is my solution.