// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G'];
return dnaBases[Math.floor(Math.random() * 4)];
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = []
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
// Returns a new instance of a pAequor object
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() {
// used to select random base in the array
let randomBaseIndex = Math.floor(Math.random() * 15);
let mutation;
// creates the mutation of a random base
switch (this.dna[randomBaseIndex]) {
case 'A':
do {
mutation = returnRandBase();
} while (mutation === 'A');
break;
case 'T':
do {
mutation = returnRandBase();
} while (mutation === 'T');
break;
case 'C':
do {
mutation = returnRandBase();
} while (mutation === 'C');
break;
case 'G':
do {
mutation = returnRandBase();
} while (mutation === 'G');
break;
default:
console.log("Invalid base");
}
// mutates the selected base and returns the mutated strain
this.dna[randomBaseIndex] = mutation;
return this.dna;
},
compareDNA(pAequor) {
let counter = 0;
// finds the coincidences in dna between pAequors
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === pAequor.dna[i]) {
counter++
}
}
let percentage = (counter / 15) * 100;
console.log(`Specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percentage}% DNA in common.`);
},
willLikelySurvive() {
let counter = 0;
for (const base of this.dna) {
if (base === 'C' || base === 'G') { counter++ }
}
let containsGoodBasesPercentage = (counter / 15) * 100;
if (containsGoodBasesPercentage >= 60) {
return true;
} else {
return false;
}
}
}
}
// Array of viable pAequor for later study
const viableCandidates = [];
// Request: 30 pAequor that can survive their natural environment
for (let i = 0; i < 30; i++) {
let testSubject;
// gets a viable candidate for the study
do {
testSubject = pAequorFactory(i, mockUpStrand());
} while (testSubject.willLikelySurvive() === false);
// sends it to the array of pAequors
viableCandidates.push(testSubject);
}
console.log(viableCandidates);
Interesting to see the differences in respect to how we all handled the mutation, etc. For instance, I liked how mkling4 avoided hard-coding the number 15 (which at first might seem kind of random) and instead resorted to using the .length property to count the # of bases.
Holy cow! That was hard to complete. But I did it and I am proud of it! The only influence I had was from @dinar87. I looked at his code to see how he did the last part of the project. Where I had to create the 30 instances that would likely survive. However, other than that I did everything else by myself!
Interesting, so in your code you picked at random base index to change in ‘mutate’ while checked for each base letter and changed it at a random index.
But when I was writing it I was trying to come up with a way to select a random base index in the array, then checking the randomly selected base on that array against a variable that had a new random base (newBase) . If the newBase was equal to the random index base, then newBase would get assigned a new value. And it would be checked again against the random index base. If the loop evaluated to false and the newBase variable is not equal to the randomly selected base, then a splice would happen where the random index base was and it would add the newBase variable.
I hope I explained that well, let me know if you have any questions.
Sharing my solution for Mysterious Organism - a bit long winded on review of the CC solution!
Haven’t completed the final extension as I’m struggling with how to use .compareDna() as part of the solution as it specifies. I’m assuming it’s the 30 instances that were batch created that need to be compared.
Please let me know how you went with this if completed.