// 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 = (num, arr) => {
return {
specimenNum: num,
dna: arr,
mutate(){
let pos = Math.floor(Math.random() * 15);
let n = this.dna[pos];
let x = '';
do {
x = returnRandBase();
} while (n === x);
if (n !== x) {
this.dna.splice(pos, 1, x);
}
return this.dna;
},
compareDNA(obj) {
let results = 0
for (let i = 0; i < 15; i++) {
if (this.dna[i] === obj.dna[i]) {
results++;
}
}
let percenti = results/obj.length * 100
let percent = (results/obj.length * 100).toFixed(2);
return `The two DNA strands are ${percent}% in common.`;
},
}
}
const strand1 = pAequorFactory([ 'A', 'T', 'G', 'C', 'T', 'C', 'G', 'T', 'A', 'T', 'A', 'A', 'C', 'C', 'G' ]);
const strand2 = pAequorFactory(2, [ 'A', 'T', 'A', 'C', 'C', 'G', 'A', 'C', 'A', 'G', 'A', 'G', 'C', 'C', 'G' ]);
console.log(strand1.compareDNA(strand2))
Hey so I am currently trying to compare the two strands in a method, I feel like I have created the right method, but for some reason my console is throwing up an error message that say
’ if (this.dna[i] === obj.dna[i]) {
^
TypeError: Cannot read property ‘0’ of undefined’
https://www.codecademy.com/practice/projects/mysterious-organism
Im assuming it is not registering that ‘this.dna’ will be a future array but unsure how I can get around this or if it is something completely different! Any help appreciated.