Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 14, 2021 20:33
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 codecademydev/1e44200ed2d6a5023992a6402a5effb3 to your computer and use it in GitHub Desktop.
Save codecademydev/1e44200ed2d6a5023992a6402a5effb3 to your computer and use it in GitHub Desktop.
Codecademy export
// 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 = (pAequorNumber, bases) => {
let pAequor = {
specimenNum: pAequorNumber,
dna: bases,
mutate() {
const randBase = Math.floor(Math.random() * 15);
const currentBase = this.dna[randBase];
let newBase = '';
const dnaBases = ['A', 'T', 'C', 'G'];
do {
newBase = dnaBases[Math.floor(Math.random() * 4)];
} while(newBase === currentBase);
this.dna[randBase] = newBase;
},
compareDNA(pAequor) {
let commonCount = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === pAequor.dna[i]) {
commonCount++;
}
}
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${Math.floor(commonCount/this.dna.length*100)}% DNA in common`);
},
willLikelySurvive() {
let survivalCount = 0;
const survivalBar = 0.6;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === 'C' || this.dna[i] === 'G') {
survivalCount++;
}
}
return survivalCount/this.dna.length >= survivalBar;
},
complementStrand() {
let complements = {
A: 'T',
T: 'A',
C: 'G',
G: 'C'
}
let complementStrand = [];
for (let base of this.dna) {
complementStrand.push(complements[base]);
}
return complementStrand;
}
};
return pAequor;
}
const create30InstancesOfPAequor = () => {
let pAequorArray = [];
for (let i = 0; i < 30; i++) {
pAequorArray.push(pAequorFactory(i, mockUpStrand()));
}
return pAequorArray;
}
let pAequorArr = create30InstancesOfPAequor();
for(let i = 0; i < pAequorArr.length; i++) {
for(let j = 0; j < pAequorArr.length; j++) {
pAequorArr[i].compareDNA(pAequorArr[j]);
}
}
// #6, #12 have 73% commonality.
let pAequor0 = pAequorFactory(0, mockUpStrand());
console.log(pAequor0.dna);
console.log(pAequor0.complementStrand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment