Created
July 10, 2022 13:15
-
-
Save shen200001/54181a03ec43027489e01a63660d6ba7 to your computer and use it in GitHub Desktop.
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; | |
}; | |
// Factory function to create new specimen. | |
const pAequorFactory = (specimenNum, dna) => { | |
return { | |
specimenNum: specimenNum, | |
dna: dna, | |
// Function to mutate one DNA base at random. | |
mutate() { | |
let i = Math.floor(Math.random() * (this.dna.length - 1)); | |
let newBase = []; | |
do { | |
newBase = returnRandBase() | |
} while (newBase === this.dna[i]); | |
this.dna[i] = newBase; | |
return this.dna; | |
}, | |
//function to compare DNA to this DNA. | |
compareDNA (pAequor) { | |
let commonDNA = []; | |
let comDNAPercent = 0; | |
for (let i = 0; i < pAequor.dna.length; i++) { | |
if (this.dna[i] === pAequor.dna[i]) { | |
commonDNA.push(pAequor.dna[i]); | |
}; | |
}; | |
comDNAPercent = Math.round((commonDNA.length / pAequor.dna.length) * 100); | |
console.log(`Specimen #${this.specimenNum} and Specimen #${pAequor.specimenNum} have ${comDNAPercent}% DNA in common.`); | |
}, | |
// Function to test if pAequor will likely survive. | |
willLikelySurvive () { | |
let cOrG = this.dna.filter(base => { | |
return base === 'C' || base === 'G'; | |
}); | |
let percent = Math.round((cOrG.length / this.dna.length) * 100); | |
if (percent >= 60) { | |
return true; | |
} else { | |
return false; | |
}; | |
}, | |
complementStrand () { | |
let compStrand = this.dna.map(base => { | |
if (base === 'A') { | |
return 'T'; | |
} else if (base === 'T') { | |
return 'A'; | |
} else if (base === 'G') { | |
return 'C'; | |
} else if (base === 'C') { | |
return 'G'; | |
}; | |
}); | |
return compStrand; | |
}, | |
}; | |
}; | |
// Function to create a number of specimens. | |
const makeSpecimens = (num) => { | |
let specimenArray = []; | |
for (let i = 1; i <= num; i++) { | |
specimenArray.push(pAequorFactory(i, mockUpStrand())); | |
}; | |
return specimenArray; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment