Skip to content

Instantly share code, notes, and snippets.

@abbieruth
Created October 11, 2021 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abbieruth/f973097565b2cc92b7e3331c679b48a3 to your computer and use it in GitHub Desktop.
Save abbieruth/f973097565b2cc92b7e3331c679b48a3 to your computer and use it in GitHub Desktop.
Mysterious Organism
// 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
};
//Simulate a P. Aequor specimen
function pAequorFactory(num, array) {
return {
specimenNum: num,
dna: array,
mutate() {
let i = Math.floor(Math.random() * this.dna.length);
let original = this.dna[i];
let bases = ['A', 'T', 'C', 'G'];
bases.splice(bases.indexOf(original), 1);
let mutator = bases[Math.floor(Math.random() * 3)];
return this.dna.splice(i, 1, mutator);
},
//Compare DNA strands.
compareDNA(pAequor) {
let sameBases = 0;
let totalBases = this.dna.length + pAequor.dna.length;
console.log(this.dna);
console.log(pAequor.dna);
for (let i = this.dna.length - 1; i > 0; i--) {
if (this.dna[i] === pAequor.dna[i]) {
sameBases += 1;
}
}
let percentage = ((sameBases / totalBases) * 100).toFixed();
console.log(`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentage}% DNA in common.`);
},
//Determine if the specimen is likely to survive
willLikelySurvive() {
let cgBases = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === 'C' || this.dna[i] === 'G') {
cgBases++;
}
}
let percent = ((cgBases / 15) * 100);
if (percent >= 60) {
return true;
} else {
return false;
}
},
//Retrun a DNA strand exactly opposite to the current one
complementStrand() {
let strand1 = this.dna;
console.log(strand1);
let strand2 = [];
for (let i = 0; i < strand1.length; i++) {
if (strand1[i] === 'A') {
strand2.push('T');
} else if (strand1[i] === 'T') {
strand2.push('A');
} else if (strand1[i] === 'G') {
strand2.push('C');
} else if (strand1[i] === 'C') {
strand2.push('G');
}
}
console.log(strand2);
}
}
}
pAequorFactory(1, mockUpStrand()).complementStrand();
//Create batch of 30 viable specimens
const viableBatch = [];
let specimenCounter = 1;
while (viableBatch.length < 30) {
let newPaequor = pAequorFactory(specimenCounter, mockUpStrand());
if (newPaequor.willLikelySurvive()) {
viableBatch.push(newPaequor);
}
specimenCounter++;
}
//console.log(viableBatch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment