In the pAequorFactory()
, when invoking that function to create an object. I want the .specimenNum
to be unique for all objects. Check the code below
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() {
randomBaseIndex = Math.floor[Math.random * 15];
mutatedBase = returnRandBase();
if (this.dna[randomBaseIndex] !== mutatedBase) {
this.dna[randomBaseIndex] === mutatedBase;
return this.dna;
} else {
mutate();
}
},
compareDNA(object) {
for (i = 0; i < 15; i++) {
let j = 0;
if (object.dna[i] === this.dna[i]) {
j++;
}
let commonPercentage = (j / 15) * 100;
}
return `specimen #${this.specimenNum} and specimen #${object.specimenNum} have ${commonPercentage}% DNA in common`;
},
willLikelySurvive() {
for (i = 0; i < 15; i++) {
let j = 0;
if (this.dna[i] === "C" || this.dna[i] === "G") {
j++;
}
let survivePercentage = (j / 15) * 100;
}
return survivePercentage >= 60;
},
};
};
I haven’t tried anything yet but I am thinking of a way to compare .specimenNum
of each object.
2
Answers
The best way is to use closure
This will create unique object each time you call createSpecimen, unique property will be specimenNum.
You can create a global numeric value and increment it: