Hi Roy,
Here’s my code in its entirety! Also, I just noticed something super strange - when I went to delete an empty row in the mutate() method, or delete a comment that I had made earlier that just said “//test it” (which I didn’t think would affect my code?), the output completely changed from producing 30 specimens to an output that just said “”. I have no idea why, do you know?
Also I thought I’d successfully tested the compareDNA() method earlier, but looking back, the output was just from the console.log() line before it. I’m still unsure of how to test the compareDNA() method. When I tried, there was no output.
// 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;
};
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() {
//selecting random base from this dna
const randBase = Math.floor(Math.random() * this.dna.length);
//establish random base as oldBase
let oldBase = this.dna[randBase];
//establish replacement base as newBase
let newBase = returnRandBase();
//compare oldBase to newBase until they don't match and mutate
do {
newBase = returnRandBase();
} while (newBase === oldBase);
oldBase = newBase;
//console.log(`The mutated dna base has become: ${newBase}`);
//return mutated strand
return this.dna;
},
//test it!
//const strand = mockUpStrand()
//const pStrand = pAequorFactory(1, strand)
//console.log(pStrand.mutate());
compareDNA(pAequorObj) {
//compare current pAequor dna with passed in pAequor dna
let inCommon = 0;
for (let i=0; i < 15; i++) {
//compute how many bases are identicle in the same locations
if (this.dna[i] === pAequorObj.dna[i]) {
inCommon++
}
}
inCommon/=15
inCommon*=100
//print message that states % of dna the two obj have in common
//use .specimenNum to identify which pAequor objects are bring compared
console.log(`Strand ${this.specimenNum} and strand ${pAequorObj.specimenNum} have ${Math.floor(inCommon)}% DNA in common.`);
},
//test it!
//strand1 = mockUpStrand()
//strand2 = mockUpStrand()
//Now pass each to the factory and assign the returns to two variables.
//variable1 = pAequorFactory(1, strand1)
//variable2 = pAequorFactory(2, strand2)
//Next call the compare method on one with the other as argument.
//variable1.compareDNA(variable2);
willLikelySurvive() {
count = 0;
for (let i of this.dna) {
if ('CG'.includes(i)) {
count++
}
}
return count > 8
}
}
}
//test it!
//strand = mockUpStrand()
//strain = pAequorFactory(1, strand)
//console.log(strain.willLikelySurvive());
//create 30 instances of pAequor and store in an array
const specimenArray = [];
let i = 1;
let newSpecimen = pAequorFactory(i, mockUpStrand());
for (let i=1; i<=30; i++) {
if (newSpecimen.willLikelySurvive() === true) {
specimenArray.push(pAequorFactory(i, mockUpStrand()));
} else if (newSpecimen.willLikelySurvive() === false) {
specimenArray.pop();
}
}
console.log(specimenArray);
Output:
[
{
specimenNum: 1,
dna: [
‘C’, ‘C’, ‘T’, ‘C’,
‘G’, ‘C’, ‘T’, ‘A’,
‘A’, ‘C’, ‘G’, ‘T’,
‘T’, ‘G’, ‘T’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 2,
dna: [
‘G’, ‘T’, ‘G’, ‘T’,
‘G’, ‘C’, ‘C’, ‘G’,
‘A’, ‘A’, ‘G’, ‘A’,
‘T’, ‘C’, ‘G’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 3,
dna: [
‘C’, ‘T’, ‘A’, ‘C’,
‘G’, ‘C’, ‘G’, ‘T’,
‘G’, ‘A’, ‘A’, ‘G’,
‘A’, ‘C’, ‘G’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
{
specimenNum: 4,
dna: [
‘A’, ‘A’, ‘A’, ‘A’,
‘A’, ‘A’, ‘T’, ‘C’,
‘G’, ‘A’, ‘G’, ‘T’,
‘C’, ‘G’, ‘A’
],
mutate: [Function: mutate],
compareDNA: [Function: compareDNA],
willLikelySurvive: [Function: willLikelySurvive]
},
etc etc etc
Thanks!
Megan