Skip to content

Instantly share code, notes, and snippets.

@peeczi
Created February 2, 2022 21:03
Show Gist options
  • Save peeczi/e58a7ceff757d75468824b229047321a to your computer and use it in GitHub Desktop.
Save peeczi/e58a7ceff757d75468824b229047321a to your computer and use it in GitHub Desktop.
mysterious organism exercise by DNA sequences likely to survive
// 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;
};
// returns a specimen with its DNA, comparison with another DNA, and the CG ratio in given DNA
const pAequorFactory = (number, array) => {
return {
specimenNum: number,
dna: array,
mutate: function() {
// sees if there's at least two DNA bases in order to start comparing them so mutation can correctly begin
if (this.dna.length <= 2) {
this.dna.push(returnRandBase());
}
// keep adding bases until DNA strand reaches 15
for (let i = 0; this.dna.length <= 14; i ++) {
//console.log ('adding another base..');
// add new DNA base to dna array
this.dna.push(returnRandBase());
// if last two items are identical overwrite last item
if (this.dna[this.dna.length-1] === this.dna[this.dna.length-2]) {
this.dna[this.dna.length-1] = returnRandBase();
}
}
//console.log(`DNA: ${this.dna}`);
return this.dna;
},
// compare two separate DNA samples to identify similarities
compareDNA: function(array) {
// number variable to count DNA samples' similarities
let identicalDNA = 0;
// increment specimen number by one to disintguish given DNA from previous
let pAequorSpecimenNum = this.specimenNum + 1;
console.log (`pAequor: ${array}`);
console.log (`this.dna: ${this.dna}`);
console.log ('identical DNA elements (0-indexed):');
// iterate through DNA strand to compare DNA bases until it reaches end of one
for (let i = 0; i <= array.length-1; i++) {
if (array[i] === this.dna[i]) {
console.log (`${array[i]} at array element ${i}`);
identicalDNA ++;
}
}
// variable to calculate percentage of DNA similarities
identicalDNA = ((identicalDNA/array.length)*100).toFixed(0);
console.log (`specimen #${this.specimenNum} and specimen #${pAequorSpecimenNum} have ${identicalDNA}% DNA in common`);
},
// method to determine whether a give DNA sample will survive based on number of 'C' and/or 'G' bases
willLikelySurvive: function() {
let surviveOr = 0;
this.dna.forEach(function(element) {
// check to see if the current DNA base in the strands' iteration has a 'C' or 'G' base
if (element === 'C' || element === 'G') {
surviveOr++;
}
})
// variable to calculate 'C' or 'G' percentage of entire DNA strand
surviveOr = ((surviveOr/this.dna.length)*100).toFixed(0);
console.log (`CG ratio: ${surviveOr}%`);
if (surviveOr >= 60) {
console.log ('You are a survivor!');
return true;
} else {
return false;
}
},
};
};
let survivorArray = [];
let survivor = 0;
let specNum = 1;
do {
let dnaArray = [];
let pFactory = pAequorFactory(specNum, dnaArray);
pFactory.mutate();
// test current DNA's ability to survive
let survivOr = pFactory.willLikelySurvive();
// if survive function returns true
if (survivOr) {
// increment survivor number
survivor ++;
// add current survivor to survivor array
survivorArray.push(pFactory.specimenNum, pFactory.dna);
// declare a new suvivor has been added
console.log ('Survivor added');
// increment specimen number for next specimen to be tested
specNum ++;
}
console.log (pFactory.specimenNum, pFactory.dna);
}
while (survivor <= 30 && specNum <= 30)
// display array of all survived specimens
console.log (`survivor list: ${survivorArray}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment