// 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 random = Math.floor(Math.random() * 15);
let randomBase = this.dna[random];
let randStand = returnRandBase();
for (let r = 0; r < randStand.length; r++){
if (randomBase[random] !== randStand[r]){
this.dna[random] = randStand[r];
} else {
false;
}
return this.dna
}
},
compareDNA(otherDna) {
let count = 0;
for (let i = 0; i < this.dna.length; i++){
if (this.dna[i] === otherDna.dna[i]){
count++
}
}
let percent = (count / this.dna.length * 100).toFixed();
return `${this.specimenNum} and ${otherDna.specimenNum} have ${percent}% DNA in commom.`;
},
willLikelySurvive(){
let base = 0;
for (let b = 0; b <= this.dna.length; b++){
if (this.dna[b] === 'C' || this.dna[b] === 'G'){
base++;
}
}
let chance = ((base / this.dna.length) * 100).toFixed();
if (chance > 59) {
return true;
} else {
return false;
}
},
complementStrand(){
let complementDNA = this.dna.map(element => {
switch (element) {
case 'A' : return 'T';
break;
case 'T' : return 'A';
break;
case 'G': return 'C';
break;
case 'C': return 'G';
break;
}
})
return complementDNA;
}
};
}
const tests = () => {
let survivors = ;
let x = 1;
while (survivors.length < 30) {
let test = pAequorFactory(x, mockUpStrand());
x++;
if (test.willLikelySurvive === true){
survivors.push(test);
}
} return survivors;
}
What am I doing wrong on getting the 30 instances?!?
Can you specify your problem, please? What do you mean by 30 instances (of what after doing what)?
On first glance, your factory function seems to do what it should.
Calling this just creates one instance of pAequor.
on this part. We have to create 30 instances that the organism will survive. So this it suppose to run the pAequorFactory a bunch of times till there are 30 paequor in the arr. However when I run it it dosen’t show any surviving. I checked my willLikelySurvive function and it works so it has to be the tests function.
is never true.
If you console.log(test.willLikelySurvive) you’ll see that it prints the whole function, because you forgot the parenthesis.
You also push a function to the survivors array, because this pAequorFactory(x, mockUpStrand()) just stores the function.