Skip to content

Instantly share code, notes, and snippets.

@suzuran7053
Last active December 5, 2021 09:27
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 suzuran7053/376b622f2e2dc4902b30669288ac7e53 to your computer and use it in GitHub Desktop.
Save suzuran7053/376b622f2e2dc4902b30669288ac7e53 to your computer and use it in GitHub Desktop.
// 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
}
// 3
function pAequorFactory(num, arr){
let obj = {
specimenNum: num,
dna: arr
}
// 4
obj.mutate = function (){
let arr = this.dna;
let len = arr.length;
let randomBase = arr[Math.floor(Math.random() * len)];
let changeToBase = arr[Math.floor(Math.random() * len)];
while(randomBase == changeToBase){
chaangeToBase = arr[Math.floor(Math.random() * len)];
}
let changedArr = arr.map(ele => {
if(ele == randomBase){
return changeToBase;
}
return ele;
})
return changedArr;
},
//5
obj.compareDNA = function(obj){
let thisDna = this.dna;
let passedDna = obj.dna;
let commons = 0;
for(let i = 0 ; i < thisDna.length; i++){
if(thisDna[i] == passedDna[i]){
commons++;
}
}
let per = Math.floor(commons / thisDna.length * 100);
console.log(`specimen #1 and specimen #2 have ${per}% DNA in common`);
},
// 6
obj.willLikelySurvive = function(){
let gc = this.dna.filter(ele => {
return ele == "C" || ele == "G";
})
return Math.floor(gc.length / this.dna.length * 100) >= 60;
}
return obj;
}
console.log(pAequorFactory(returnRandBase(), mockUpStrand()));
/* Example Output
{ specimenNum: 'T',
dna: [ 'G', 'T', 'G', 'A', 'G', 'T', 'T', 'G', 'G', 'C', 'A', 'T', 'C', 'A', 'C' ],
mutate: [Function],
compareDNA: [Function],
willLikelySurvive: [Function] }
*/
// 7
let pAequors = [];
while(pAequors.length < 30){
let specimen = pAequorFactory(returnRandBase(), mockUpStrand());
if(specimen.willLikelySurvive()){
pAequors.push(specimen);
}
}
console.log(pAequors);
// Extra Practice
// CHECK WEATHER ALL THE INSTANCES IN pAequors CAN SURVIVE
let canSurvive = pAequors.filter(x => {
return !x.willLikelySurvive();
})
if (canSurvive.length == 0){
console.log("They all can survive");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment