-
-
Save codecademydev/b3fad04080f73849353d8fbe0582c201 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
// make an instance of P.aequor | |
const pAequorFactory = (number, dnaStrand) => { | |
return { | |
specimenNum: number, | |
dna: dnaStrand, | |
mutate(){ | |
const randomBaseNum = Math.floor(Math.random() * this.dna.length); | |
const dnaBases = ['A', 'T', 'C', 'G'].filter(base => base !== this.dna[randomBaseNum]); | |
this.dna[randomBaseNum] = dnaBases[Math.floor(Math.random() * 3)] ; | |
}, | |
compareDNA(pAequor){ | |
let commonDna = 0; | |
for(let i = 0; i < this.dna.length; i++){ | |
if(this.dna[i] === pAequor.dna[i]){ | |
commonDna++; | |
} | |
} | |
const percentInCommon = (Math.floor(commonDna/this.dna.length * 10000)) / 100; | |
return percentInCommon; | |
// console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percentInCommon}% DNA in common`); | |
}, | |
willLikelySurvive(){ | |
let baseCsOrGs = 0; | |
for(let i = 0; i < this.dna.length; i++){ | |
if(this.dna[i] === 'C' || this.dna[i] === 'G'){ | |
baseCsOrGs++; | |
} | |
} | |
return baseCsOrGs / this.dna.length >= 0.60; | |
}, | |
complementStrand(){ | |
const compStrand = []; | |
for(let i = 0; i < this.dna.length; i++){ | |
switch(this.dna[i]){ | |
case 'A': | |
compStrand.push('T'); | |
break; | |
case 'T': | |
compStrand.push('A'); | |
break; | |
case 'C': | |
compStrand.push('G'); | |
break; | |
case 'G': | |
compStrand.push('C'); | |
break; | |
default: | |
console.log('DNA Base is Invalid!'); | |
break; | |
} | |
} | |
return compStrand; | |
} | |
} | |
} | |
// make an array of pAequors (that can survive in their natural environment) | |
function createInstancesOfpAequor(NumberOfIstance){ | |
const pAequors = []; | |
let sepeciesNum = 1; | |
while(pAequors.length < NumberOfIstance){ | |
const newpAequor = pAequorFactory(sepeciesNum, mockUpStrand()); | |
if(newpAequor.willLikelySurvive()){ | |
pAequors.push(newpAequor); | |
sepeciesNum++; | |
} | |
} | |
return pAequors; | |
} | |
// find the 2 most similar instances | |
function findTheMostRelated(pAequors){ | |
const mostRelatedInstances = { | |
comPercent: 0, | |
instance1SpecNum: 0, | |
instance2SpecNum: 0 | |
}; | |
for(let i = 0 ; i < pAequors.length; i++){ | |
for(let j = i+1; j < pAequors.length; j++){ | |
const tempCommonPercent = pAequors[i].compareDNA(pAequors[j]); | |
if(tempCommonPercent > mostRelatedInstances.comPercent){ | |
mostRelatedInstances.comPercent = tempCommonPercent; | |
mostRelatedInstances.instance1SpecNum = pAequors[i].specimenNum; | |
mostRelatedInstances.instance2SpecNum = pAequors[j].specimenNum; | |
} | |
} | |
} | |
console.log('MOST RELATED INSTANCES'); | |
console.log(`specimen #${mostRelatedInstances.instance1SpecNum} and specimen #${mostRelatedInstances.instance2SpecNum} have ${mostRelatedInstances.comPercent}% DNA in common`); | |
console.log(pAequors[mostRelatedInstances.instance1SpecNum - 1]) | |
console.log(pAequors[mostRelatedInstances.instance2SpecNum - 1]) | |
return mostRelatedInstances; | |
} | |
const list = createInstancesOfpAequor(1000); | |
findTheMostRelated(list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment